-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomdb_api.module
115 lines (102 loc) · 2.95 KB
/
omdb_api.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* @file
* Provides an omdb api entity type.
*/
use Drupal\Core\Render\Element;
use Drupal\user\UserInterface;
use Drupal\omdb_api\Entity\Bundle\Series;
use Drupal\omdb_api\Entity\Bundle\Movie;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Implements hook_theme().
*/
function omdb_api_theme() {
return [
'omdb_api' => [
'render element' => 'elements',
],
];
}
/**
* Prepares variables for omdb api templates.
*
* Default template: omdb-api.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the omdb api information and
* any fields attached to the entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_omdb_api(array &$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_user_cancel().
*/
function omdb_api_user_cancel($edit, UserInterface $account, $method) {
switch ($method) {
case 'user_cancel_block_unpublish':
// Unpublish omdb apis.
$storage = \Drupal::entityTypeManager()->getStorage('omdb_api');
$omdb_api_ids = $storage->getQuery()
->condition('uid', $account->id())
->condition('status', 1)
->execute();
foreach ($storage->loadMultiple($omdb_api_ids) as $omdb_api) {
$omdb_api->set('status', FALSE);
$omdb_api->save();
}
break;
case 'user_cancel_reassign':
// Anonymize omdb apis.
$storage = \Drupal::entityTypeManager()->getStorage('omdb_api');
$omdb_api_ids = $storage->getQuery()
->condition('uid', $account->id())
->execute();
foreach ($storage->loadMultiple($omdb_api_ids) as $omdb_api) {
$omdb_api->setOwnerId(0);
$omdb_api->save();
}
break;
}
}
/**
* Implements hook_ENTITY_TYPE_predelete() for user entities.
*/
function omdb_api_user_predelete(UserInterface $account) {
// Delete omdb apis.
$storage = \Drupal::entityTypeManager()->getStorage('omdb_api');
$omdb_api_ids = $storage->getQuery()
->condition('uid', $account->id())
->execute();
$omdb_apis = $storage->loadMultiple($omdb_api_ids);
$storage->delete($omdb_apis);
// Delete old revisions.
$omdb_api_ids = $storage->getQuery()
->allRevisions()
->condition('uid', $account->id())
->execute();
foreach (array_keys($omdb_api_ids) as $revision_id) {
$storage->deleteRevision($revision_id);
}
}
/**
* Implements hook_entity_bundle_info().
*/
function omdb_api_entity_bundle_info(): array {
$bundles = [];
$bundles['omdb_api']['series'] = [
'label' => new TranslatableMarkup('Series'),
'class' => Series::class,
];
$bundles['omdb_api']['movie'] = [
'label' => new TranslatableMarkup('Movie'),
'class' => Movie::class,
];
return $bundles;
}