55 * Contains islandora_group.module.
66 */
77
8- use Drupal\Core\Entity\EntityForm;
8+ // phpcs:disable Drupal.NamingConventions.ValidFunctionName
9+
910use Drupal\Core\Routing\RouteMatchInterface;
1011use Drupal\node\NodeInterface;
1112use Drupal\group\Entity\GroupRelationship;
1213use Drupal\media\MediaInterface;
13- use Drupal\taxonomy\Entity\Term;
1414use Drupal\islandora_group\Utilities;
1515use Drupal\Core\Entity\EntityInterface;
16+
1617/**
1718 * Implements hook_help().
1819 */
1920function islandora_group_help($route_name, RouteMatchInterface $route_match) {
20- switch ($route_name) {
21- // Main module help for the islandora_group module.
22- case 'help.page.islandora_group':
23- $output = '';
24- $output .= '<h3>' . t('About') . '</h3>';
25- $output .= '<p>' . t('This module assist to manage access control for node and media with Group module') . '</p>';
26- return $output;
27-
28- default:
29- }
21+ switch ($route_name) {
22+ // Main module help for the islandora_group module.
23+ case 'help.page.islandora_group':
24+ $output = '';
25+ $output .= '<h3>' . t('About') . '</h3>';
26+ $output .= '<p>' . t('This module assist to manage access control for node and media with Group module') . '</p>';
27+ return $output;
28+
29+ default:
30+ }
3031}
3132
3233/**
3334 * Implements hook_theme().
3435 */
3536function islandora_group_theme() {
36- return [
37- 'islandora_group' => [
38- 'render element' => 'children',
39- ],
40- ];
37+ return [
38+ 'islandora_group' => [
39+ 'render element' => 'children',
40+ ],
41+ ];
4142}
4243
4344/**
44- * Implements hook_entity_insert
45- *
46- * @param \Drupal\Core\Entity\EntityInterface $entity
45+ * Implements hook_entity_insert().
4746 */
4847function islandora_group_entity_insert(EntityInterface $entity) {
49- Utilities::sync_associated_taxonomy_with_group($entity, "insert");
48+ Utilities::sync_associated_taxonomy_with_group($entity, "insert");
5049}
5150
5251/**
53- * Implements hook_entity_update
54- *
55- * @param \Drupal\Core\Entity\EntityInterface $entity
52+ * Implements hook_entity_update().
5653 */
5754function islandora_group_entity_update(EntityInterface $entity) {
58- Utilities::sync_associated_taxonomy_with_group($entity, "update");
55+ Utilities::sync_associated_taxonomy_with_group($entity, "update");
5956}
6057
6158/**
62- * Implements hook_entity_delete
63- *
64- * @param \Drupal\Core\Entity\EntityInterface $entity
59+ * Implements hook_entity_delete().
6560 */
6661function islandora_group_entity_delete(EntityInterface $entity) {
67- Utilities::sync_associated_taxonomy_with_group($entity, "delete");
62+ Utilities::sync_associated_taxonomy_with_group($entity, "delete");
6863}
6964
7065/**
71- * Implements hook_form_alter
72- * @param $form
73- * @param $form_state
74- * @param $form_id
75- * @return void
66+ * Implements hook_form_alter().
7667 */
7768function islandora_group_form_alter(&$form, &$form_state, $form_id) {
78- if (str_starts_with($form_id, "group_content_group_content_type") && str_ends_with($form_id, "_delete_form")) {
79- // when insert update
80- $form['actions']['submit']['#submit'][] = 'form_submit_delete_relation_untagging_entity_to_group';
81- }
82- else if (in_array($form_id, ['media_access_control_form', "islandora_object_access_control_form"])) {
83- $form['#attached']['library'] = array(
84- 'islandora_group/datatables',
85- ) ;
86- }
69+ if (str_starts_with($form_id, "group_content_group_content_type") && str_ends_with($form_id, "_delete_form")) {
70+ // When insert update.
71+ $form['actions']['submit']['#submit'][] = 'form_submit_delete_relation_untagging_entity_to_group';
72+ }
73+ elseif (in_array($form_id, ['media_access_control_form', "islandora_object_access_control_form"])) {
74+ $form['#attached']['library'] = [
75+ 'islandora_group/datatables',
76+ ] ;
77+ }
8778}
8879
8980/**
9081 * Implements hook_node_delete().
9182 */
9283function islandora_group_node_delete(NodeInterface $node) {
93- // Remove content from all groups.
94- foreach (GroupRelationship::loadByEntity($node) as $group_content) {
95- $group_content->delete();
96- }
84+ // Remove content from all groups.
85+ foreach (GroupRelationship::loadByEntity($node) as $group_content) {
86+ $group_content->delete();
87+ }
9788}
9889
9990/**
10091 * Implements hook_media_delete().
10192 */
10293function islandora_group_media_delete(MediaInterface $media) {
103- foreach (GroupRelationship::loadByEntity($media) as $group_content) {
104- $group_content->delete();
105- }
94+ foreach (GroupRelationship::loadByEntity($media) as $group_content) {
95+ $group_content->delete();
96+ }
10697}
10798
10899/**
109100 * Implements hook_node_insert().
110101 */
111102function islandora_group_node_insert(NodeInterface $node) {
112- Utilities::print_log("islandora_group_node_insert");
113- // add this node to group
114- if (!Utilities::isCalledFromGroupModule()) {
115- Utilities::adding_islandora_object_to_group($node);
116- }
103+ Utilities::print_log("islandora_group_node_insert");
104+ // Add this node to group.
105+ if (!Utilities::isCalledFromGroupModule()) {
106+ Utilities::adding_islandora_object_to_group($node);
107+ }
117108}
118109
119110/**
120111 * Implements hook_node_update().
121112 */
122113function islandora_group_node_update(NodeInterface $node) {
123- Utilities::print_log("islandora_group_node_update");
124- if (!Utilities::isCalledFromGroupModule()) {
125- Utilities::adding_islandora_object_to_group($node);
126-
127- // Tag media with selected terms only while bulk batch update
128- if (Utilities::isCalledFromBulkBatch()) {
129- // get access control field from config
130- $access_control_field = Utilities::getAccessControlFieldinNode($node);
131-
132- // make sure the selected access control field valid
133- if (empty($access_control_field) || !$node->hasField($access_control_field) ) {
134- return;
135- }
136-
137- $node_terms = $node->get($access_control_field)->referencedEntities();
138- $targets = [];
139- foreach ($node_terms as $term) {
140- $targets[] = ['target_id' => $term->id()];
141- }
142- $medias = [];
143- if (!empty(\Drupal::hasService('islandora.utils'))) {
144- $medias = \Drupal::service('islandora.utils')->getMedia($node);
145- }
146- $other_medias = Utilities::getMedia($node);
147- if (count($other_medias) > 0) {
148- $medias = array_merge($medias, $other_medias);
149- }
150- foreach ($medias as $media) {
151- Utilities::taggingFieldAccessTermMedia($media, $targets);
152- }
153- }
114+ Utilities::print_log("islandora_group_node_update");
115+ if (!Utilities::isCalledFromGroupModule()) {
116+ Utilities::adding_islandora_object_to_group($node);
117+
118+ // Tag media with selected terms only while bulk batch update.
119+ if (Utilities::isCalledFromBulkBatch()) {
120+ // Get access control field from config.
121+ $access_control_field = Utilities::getAccessControlFieldinNode($node);
122+
123+ // Make sure the selected access control field valid.
124+ if (empty($access_control_field) || !$node->hasField($access_control_field)) {
125+ return;
126+ }
127+
128+ $node_terms = $node->get($access_control_field)->referencedEntities();
129+ $targets = [];
130+ foreach ($node_terms as $term) {
131+ $targets[] = ['target_id' => $term->id()];
132+ }
133+ $medias = [];
134+ if (!empty(\Drupal::hasService('islandora.utils'))) {
135+ $medias = \Drupal::service('islandora.utils')->getMedia($node);
136+ }
137+ $other_medias = Utilities::getMedia($node);
138+ if (count($other_medias) > 0) {
139+ $medias = array_merge($medias, $other_medias);
140+ }
141+ foreach ($medias as $media) {
142+ Utilities::taggingFieldAccessTermMedia($media, $targets);
143+ }
154144 }
145+ }
155146}
156147
157148/**
158149 * Implements hook_media_insert().
159150 */
160151function islandora_group_media_insert(MediaInterface $media) {
161- Utilities::print_log("islandora_group_media_insert");
162- // add this node to group
163- if (!Utilities::isCalledFromGroupModule()) {
164- Utilities::adding_media_only_into_group($media);
165- }
152+ Utilities::print_log("islandora_group_media_insert");
153+ // Add this node to group.
154+ if (!Utilities::isCalledFromGroupModule()) {
155+ Utilities::adding_media_only_into_group($media);
156+ }
166157}
167158
168159/**
169160 * Implements hook_media_update().
170161 */
171162function islandora_group_media_update(MediaInterface $media) {
172- Utilities::print_log("islandora_group_media_update");
173- // add this node to group
174- if (!Utilities::isCalledFromGroupModule()) {
175- Utilities::adding_media_only_into_group($media);
176- }
163+ Utilities::print_log("islandora_group_media_update");
164+ // Add this node to group.
165+ if (!Utilities::isCalledFromGroupModule()) {
166+ Utilities::adding_media_only_into_group($media);
167+ }
177168}
178169
179170/**
180171 * Batch 'finished' callback used by both batch 1 and batch 2.
181172 */
182173function islandora_group_batch_finished($success, $results, $operations) {
183- $messenger = \Drupal::messenger();
184- if ($success) {
185- // Here we could do something meaningful with the results.
186- // We just display the number of nodes we processed...
187- $messenger->addMessage(t('Access control applied successfully.', ['@total' => count($operations)]));
188- }
189- else {
190- // An error occurred.
191- // $operations contains the operations that remained unprocessed.
192- $error_operation = reset($operations);
193- $messenger->addMessage(
194- t('An error occurred while applying @operation with arguments : @args',
195- [
196- '@operation' => $error_operation[0],
197- '@args' => print_r($error_operation[0], TRUE),
198- ]
199- )
200- );
201- }
174+ $messenger = \Drupal::messenger();
175+ if ($success) {
176+ // Here we could do something meaningful with the results.
177+ // We just display the number of nodes we processed...
178+ $messenger->addMessage(t('Access control applied successfully.', ['@total' => count($operations)]));
179+ }
180+ else {
181+ // An error occurred.
182+ // $operations contains the operations that remained unprocessed.
183+ $error_operation = reset($operations);
184+ $messenger->addMessage(
185+ t('An error occurred while applying @operation with arguments : @args',
186+ [
187+ '@operation' => $error_operation[0],
188+ '@args' => print_r($error_operation[0], TRUE),
189+ ]
190+ )
191+ );
192+ }
202193}
203194
204195/**
205196 * Check if an entity type has a field.
206- * https://www.drupal.org/forum/support/post-installation/2020-06-25/how-to-check-if-field-exists-for-entitytypemanager
197+ *
198+ * - https://www.drupal.org/forum/support/post-installation/2020-06-25/how-to-check-if-field-exists-for-entitytypemanager.
199+ *
207200 * @param string $entity_type
208201 * The entity type.
209202 * @param string $field_name
@@ -215,7 +208,7 @@ function islandora_group_batch_finished($success, $results, $operations) {
215208function entityTypeHasField($entity_type, $field_name) {
216209 $bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type);
217210
218- foreach($bundles as $bundle => $label) {
211+ foreach ($bundles as $bundle => $label) {
219212 $all_bundle_fields = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type, $bundle);
220213 if (isset($all_bundle_fields[$field_name])) {
221214 return TRUE;
0 commit comments