@@ -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