-
Notifications
You must be signed in to change notification settings - Fork 5
/
embargo.module
156 lines (138 loc) · 4.55 KB
/
embargo.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* @file
* Hook implementations.
*/
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\embargo\EmbargoStorage;
use Drupal\media\MediaInterface;
use Drupal\node\NodeInterface;
/**
* Implements hook_entity_type_alter().
*/
function embargo_entity_type_alter(array &$entity_types) : void {
$applicable_entity_types = EmbargoStorage::applicableEntityTypes();
foreach ($applicable_entity_types as $entity_type_id) {
$entity_type = &$entity_types[$entity_type_id];
$entity_type->setLinkTemplate('embargoes', "/embargo/$entity_type_id/{{$entity_type_id}}");
}
}
/**
* Implements hook_entity_access().
*/
function embargo_entity_access(EntityInterface $entity, $operation, AccountInterface $account) : AccessResultInterface {
/** @var \Drupal\embargo\Access\EmbargoAccessCheckInterface $service */
$service = \Drupal::service('access_check.embargo');
return $service->access($entity, $account);
}
/**
* Implements hook_file_download().
*/
function embargo_file_download($uri) : null|array|int {
$files = \Drupal::entityTypeManager()
->getStorage('file')
->loadByProperties(['uri' => $uri]);
$file = reset($files);
if ($file instanceof EntityInterface) {
$access = \Drupal::service('access_check.embargo')->access($file, \Drupal::currentUser());
if ($access->isForbidden()) {
return -1;
}
}
return NULL;
}
/**
* Implements hook_query_TAG_alter() for `node_access` tagged queries.
*/
function embargo_query_node_access_alter(AlterableInterface $query) : void {
/** @var \Drupal\embargo\Access\QueryTagger $tagger */
$tagger = \Drupal::service('embargo.query_tagger');
$tagger->tagNode($query);
}
/**
* Implements hook_theme().
*/
function embargo_theme($existing, $type, $theme, $path) : array {
return [
'embargo_ip_access_exemption' => [
'template' => 'embargo-ip-access-exemption',
'variables' => [
// A list of resource names.
'resources' => [],
// A list of embargo_ip_range entity identifers.
'ranges' => [],
'contact_email' => NULL,
],
],
'embargo_notification' => [
'template' => 'embargo-notification',
'variables' => [
'embargoes' => [],
'message' => NULL,
],
],
];
}
/**
* Implements hook_ENTITY_TYPE_insert() for embargo entities.
*/
function embargo_embargo_insert(EntityInterface $entity) : void {
/** @var \Drupal\embargo\SearchApiTracker $tracker */
$tracker = \Drupal::service('embargo.search_api_tracker_helper');
$tracker->track($entity);
}
/**
* Implements hook_ENTITY_TYPE_update() for embargo entities.
*/
function embargo_embargo_update(EntityInterface $entity) : void {
/** @var \Drupal\embargo\SearchApiTracker $tracker */
$tracker = \Drupal::service('embargo.search_api_tracker_helper');
$tracker->track($entity);
}
/**
* Implements hook_ENTITY_TYPE_delete() for embargo entities.
*/
function embargo_embargo_delete(EntityInterface $entity) : void {
/** @var \Drupal\embargo\SearchApiTracker $tracker */
$tracker = \Drupal::service('embargo.search_api_tracker_helper');
$tracker->track($entity);
}
/**
* Implements hook_ENTITY_TYPE_delete() for node entities.
*/
function embargo_node_delete(EntityInterface $entity) : void {
assert($entity instanceof NodeInterface);
/** @var \Drupal\embargo\SearchApiTracker $tracker */
$tracker = \Drupal::service('embargo.search_api_tracker_helper');
$tracker->propagateChildren($entity);
}
/**
* Implements hook_ENTITY_TYPE_insert() for media entities.
*/
function embargo_media_insert(EntityInterface $entity) : void {
assert($entity instanceof MediaInterface);
/** @var \Drupal\embargo\SearchApiTracker $tracker */
$tracker = \Drupal::service('embargo.search_api_tracker_helper');
$tracker->mediaWriteReaction($entity);
}
/**
* Implements hook_ENTITY_TYPE_update() for media entities.
*/
function embargo_media_update(EntityInterface $entity) : void {
assert($entity instanceof MediaInterface);
/** @var \Drupal\embargo\SearchApiTracker $tracker */
$tracker = \Drupal::service('embargo.search_api_tracker_helper');
$tracker->mediaWriteReaction($entity);
}
/**
* Implements hook_ENTITY_TYPE_delete() for media entities.
*/
function embargo_media_delete(EntityInterface $entity) : void {
assert($entity instanceof MediaInterface);
/** @var \Drupal\embargo\SearchApiTracker $tracker */
$tracker = \Drupal::service('embargo.search_api_tracker_helper');
$tracker->mediaDeleteReaction($entity);
}