-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathislandora_group.module
More file actions
219 lines (196 loc) · 6.2 KB
/
Copy pathislandora_group.module
File metadata and controls
219 lines (196 loc) · 6.2 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* @file
* Contains islandora_group.module.
*/
// phpcs:disable Drupal.NamingConventions.ValidFunctionName
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\NodeInterface;
use Drupal\group\Entity\GroupRelationship;
use Drupal\media\MediaInterface;
use Drupal\islandora_group\Utilities;
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_help().
*/
function islandora_group_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the islandora_group module.
case 'help.page.islandora_group':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('This module assist to manage access control for node and media with Group module') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function islandora_group_theme() {
return [
'islandora_group' => [
'render element' => 'children',
],
];
}
/**
* Implements hook_entity_insert().
*/
function islandora_group_entity_insert(EntityInterface $entity) {
Utilities::sync_associated_taxonomy_with_group($entity, "insert");
}
/**
* Implements hook_entity_update().
*/
function islandora_group_entity_update(EntityInterface $entity) {
Utilities::sync_associated_taxonomy_with_group($entity, "update");
}
/**
* Implements hook_entity_delete().
*/
function islandora_group_entity_delete(EntityInterface $entity) {
Utilities::sync_associated_taxonomy_with_group($entity, "delete");
}
/**
* Implements hook_form_alter().
*/
function islandora_group_form_alter(&$form, &$form_state, $form_id) {
if (str_starts_with($form_id, "group_content_group_content_type") && str_ends_with($form_id, "_delete_form")) {
// When insert update.
$form['actions']['submit']['#submit'][] = 'form_submit_delete_relation_untagging_entity_to_group';
}
elseif (in_array($form_id, ['media_access_control_form', "islandora_object_access_control_form"])) {
$form['#attached']['library'] = [
'islandora_group/datatables',
];
}
}
/**
* Implements hook_node_delete().
*/
function islandora_group_node_delete(NodeInterface $node) {
// Remove content from all groups.
foreach (GroupRelationship::loadByEntity($node) as $group_content) {
$group_content->delete();
}
}
/**
* Implements hook_media_delete().
*/
function islandora_group_media_delete(MediaInterface $media) {
foreach (GroupRelationship::loadByEntity($media) as $group_content) {
$group_content->delete();
}
}
/**
* Implements hook_node_insert().
*/
function islandora_group_node_insert(NodeInterface $node) {
Utilities::print_log("islandora_group_node_insert");
// Add this node to group.
if (!Utilities::isCalledFromGroupModule()) {
Utilities::adding_islandora_object_to_group($node);
}
}
/**
* Implements hook_node_update().
*/
function islandora_group_node_update(NodeInterface $node) {
Utilities::print_log("islandora_group_node_update");
if (!Utilities::isCalledFromGroupModule()) {
Utilities::adding_islandora_object_to_group($node);
// Tag media with selected terms only while bulk batch update.
if (Utilities::isCalledFromBulkBatch()) {
// Get access control field from config.
$access_control_field = Utilities::getAccessControlFieldinNode($node);
// Make sure the selected access control field valid.
if (empty($access_control_field) || !$node->hasField($access_control_field)) {
return;
}
$node_terms = $node->get($access_control_field)->referencedEntities();
$targets = [];
foreach ($node_terms as $term) {
$targets[] = ['target_id' => $term->id()];
}
$medias = [];
if (!empty(\Drupal::hasService('islandora.utils'))) {
$medias = \Drupal::service('islandora.utils')->getMedia($node);
}
$other_medias = Utilities::getMedia($node);
if (count($other_medias) > 0) {
$medias = array_merge($medias, $other_medias);
}
foreach ($medias as $media) {
Utilities::taggingFieldAccessTermMedia($media, $targets);
}
}
}
}
/**
* Implements hook_media_insert().
*/
function islandora_group_media_insert(MediaInterface $media) {
Utilities::print_log("islandora_group_media_insert");
// Add this node to group.
if (!Utilities::isCalledFromGroupModule()) {
Utilities::adding_media_only_into_group($media);
}
}
/**
* Implements hook_media_update().
*/
function islandora_group_media_update(MediaInterface $media) {
Utilities::print_log("islandora_group_media_update");
// Add this node to group.
if (!Utilities::isCalledFromGroupModule()) {
Utilities::adding_media_only_into_group($media);
}
}
/**
* Batch 'finished' callback used by both batch 1 and batch 2.
*/
function islandora_group_batch_finished($success, $results, $operations) {
$messenger = \Drupal::messenger();
if ($success) {
// Here we could do something meaningful with the results.
// We just display the number of nodes we processed...
$messenger->addMessage(t('Access control applied successfully.', ['@total' => count($operations)]));
}
else {
// An error occurred.
// $operations contains the operations that remained unprocessed.
$error_operation = reset($operations);
$messenger->addMessage(
t('An error occurred while applying @operation with arguments : @args',
[
'@operation' => $error_operation[0],
'@args' => print_r($error_operation[0], TRUE),
]
)
);
}
}
/**
* Check if an entity type has a field.
*
* - https://www.drupal.org/forum/support/post-installation/2020-06-25/how-to-check-if-field-exists-for-entitytypemanager.
*
* @param string $entity_type
* The entity type.
* @param string $field_name
* The field name.
*
* @return bool
* Returns a TRUE if the entity type has the field.
*/
function entityTypeHasField($entity_type, $field_name) {
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type);
foreach ($bundles as $bundle => $label) {
$all_bundle_fields = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type, $bundle);
if (isset($all_bundle_fields[$field_name])) {
return TRUE;
}
}
return FALSE;
}