Skip to content

Commit 95900f8

Browse files
authored
Merge pull request #29 from digitalutsc/grouprel_presisted
Fix for duplication group, and Group relationship references presisted after content are moved in and out
2 parents 9c40159 + 2f6f41c commit 95900f8

2 files changed

Lines changed: 43 additions & 28 deletions

File tree

islandora_group.module

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,15 @@ function islandora_group_entity_delete(EntityInterface $entity) {
6666
* Implements hook_form_alter().
6767
*/
6868
function islandora_group_form_alter(&$form, &$form_state, $form_id) {
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';
69+
$form_object = $form_state->getFormObject();
70+
if ($form_object instanceof \Drupal\Core\Entity\EntityFormInterface) {
71+
$entity = $form_object->getEntity();
72+
if (in_array($entity->getEntityTypeId(), ['group_relationship', 'group_content']) && $form_object->getOperation() === 'delete') {
73+
$form['actions']['submit']['#submit'][] = ['\Drupal\islandora_group\Utilities', 'form_submit_delete_relation_untagging_entity_to_group'];
74+
}
7275
}
73-
elseif (in_array($form_id, ['media_access_control_form', "islandora_object_access_control_form"])) {
76+
77+
if (in_array($form_id, ['media_access_control_form', "islandora_object_access_control_form"])) {
7478
$form['#attached']['library'] = [
7579
'islandora_group/datatables',
7680
];
@@ -112,6 +116,19 @@ function islandora_group_node_insert(NodeInterface $node) {
112116
*/
113117
function islandora_group_node_update(NodeInterface $node) {
114118
Utilities::print_log("islandora_group_node_update");
119+
// Get access control field from config.
120+
$access_control_field = Utilities::getAccessControlFieldinNode($node);
121+
122+
// Make sure the selected access control field valid.
123+
if (empty($access_control_field) || !$node->hasField($access_control_field)) {
124+
return;
125+
}
126+
127+
$node_terms = $node->get($access_control_field)->referencedEntities();
128+
if (count($node_terms) ==0) {
129+
Utilities::clear_group_relation_by_entity($node);
130+
}
131+
115132
if (!Utilities::isCalledFromGroupModule()) {
116133
Utilities::adding_islandora_object_to_group($node);
117134

@@ -149,7 +166,7 @@ function islandora_group_node_update(NodeInterface $node) {
149166
* Implements hook_media_insert().
150167
*/
151168
function islandora_group_media_insert(MediaInterface $media) {
152-
Utilities::print_log("islandora_group_media_insert");
169+
Utilities::drupal_log("islandora_group_media_insert");
153170
// Add this node to group.
154171
if (!Utilities::isCalledFromGroupModule()) {
155172
Utilities::adding_media_only_into_group($media);
@@ -160,7 +177,7 @@ function islandora_group_media_insert(MediaInterface $media) {
160177
* Implements hook_media_update().
161178
*/
162179
function islandora_group_media_update(MediaInterface $media) {
163-
Utilities::print_log("islandora_group_media_update");
180+
Utilities::drupal_log("islandora_group_media_update");
164181
// Add this node to group.
165182
if (!Utilities::isCalledFromGroupModule()) {
166183
Utilities::adding_media_only_into_group($media);

src/Utilities.php

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,7 @@ public static function getMedia(NodeInterface $node) {
5757
* @throws \Drupal\Core\Entity\EntityStorageException
5858
*/
5959
public static function clear_group_relation_by_entity($entity) {
60-
// Get access control field from config.
61-
if ($entity->getEntityTypeId() === "node") {
62-
// Get access control field from config.
63-
$access_control_field = self::getAccessControlFieldinNode($entity);
64-
}
65-
elseif ($entity->getEntityTypeId() === "media") {
66-
$access_control_field = self::getAccessControlFieldinMedia($entity);
67-
}
68-
69-
// Check if $access_control_field exists and valid.
70-
if (empty($access_control_field) || !$entity->hasField($access_control_field)) {
71-
return;
72-
}
73-
// For each term, loop through groups-entity.
60+
// Check if group relationships exist for the entity and delete them.
7461
foreach (GroupRelationship::loadByEntity($entity) as $group_content) {
7562
$group_content->delete();
7663
}
@@ -109,12 +96,12 @@ public static function taggingFieldAccessTermsNode($nid, $targets) {
10996
$node = Node::load($nid);
11097

11198
// Clear any previous relations before adding new ones if called from the UI.
112-
// 1. Clear field_access_terms in media level.
113-
self::untag_existed_field_access_terms($node);
114-
115-
// 2. Clearing group relation with islandora object.
99+
// 1. Clearing group relation with islandora object first.
116100
self::clear_group_relation_by_entity($node);
117101

102+
// 2. Clear field_access_terms.
103+
self::untag_existed_field_access_terms($node);
104+
118105
if (count($targets) > 0) {
119106
// Get access control field from config.
120107
$access_control_field = self::getAccessControlFieldinNode($node);
@@ -134,6 +121,8 @@ public static function taggingFieldAccessTermsNode($nid, $targets) {
134121
* @return void
135122
*/
136123
public static function taggingFieldAccessTermMedia($media, $targets) {
124+
// Clearing group relation with media first.
125+
self::clear_group_relation_by_entity($media);
137126

138127
self::untag_existed_field_access_terms($media);
139128

@@ -414,8 +403,16 @@ public static function adding_media_of_islandora_object_to_group($node, $media)
414403
* @return void
415404
*/
416405
public static function clear_term_in_field_access_terms($ne, $group_name) {
417-
// Get access control field from config.
418-
$access_control_field = self::getAccessControlFieldinNode($ne);
406+
// Get access control field from config based on entity type.
407+
if ($ne->getEntityTypeId() === 'node') {
408+
$access_control_field = self::getAccessControlFieldinNode($ne);
409+
}
410+
elseif ($ne->getEntityTypeId() === 'media') {
411+
$access_control_field = self::getAccessControlFieldinMedia($ne);
412+
}
413+
else {
414+
return;
415+
}
419416

420417
// @todo search if the node->field_access_terms contain group name
421418
if (empty($access_control_field) || !$ne->hasField($access_control_field)) {
@@ -498,7 +495,8 @@ public static function adding_media_only_into_group(MediaInterface $media) {
498495
// Get field_access_terms.
499496
$terms = $media->get($access_control_field)->referencedEntities();
500497
if (empty($terms)) {
501-
// No term, exit;.
498+
// No term, clear all group relationship between the media and group
499+
self::updating_media_only_into_group($media);
502500
return;
503501
}
504502

@@ -721,7 +719,7 @@ public static function form_submit_delete_relation_untagging_entity_to_group($fo
721719
$form_object = $form_state->getFormObject();
722720
if ($form_object instanceof EntityForm) {
723721
$entity = $form_object->getEntity();
724-
if ($entity->getEntityTypeId() === 'group_content') {
722+
if (in_array($entity->getEntityTypeId(), ['group_relationship', 'group_content'])) {
725723
$group_content = $entity;
726724
$group = $group_content->getGroup();
727725
if ($entity->getEntity()->getEntityTypeId() === "node") {

0 commit comments

Comments
 (0)