Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions islandora_group.module
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,15 @@ function islandora_group_entity_delete(EntityInterface $entity) {
* 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';
$form_object = $form_state->getFormObject();
if ($form_object instanceof \Drupal\Core\Entity\EntityFormInterface) {
$entity = $form_object->getEntity();
if (in_array($entity->getEntityTypeId(), ['group_relationship', 'group_content']) && $form_object->getOperation() === 'delete') {
$form['actions']['submit']['#submit'][] = ['\Drupal\islandora_group\Utilities', 'form_submit_delete_relation_untagging_entity_to_group'];
}
}
elseif (in_array($form_id, ['media_access_control_form', "islandora_object_access_control_form"])) {

if (in_array($form_id, ['media_access_control_form', "islandora_object_access_control_form"])) {
$form['#attached']['library'] = [
'islandora_group/datatables',
];
Expand Down Expand Up @@ -112,6 +116,19 @@ function islandora_group_node_insert(NodeInterface $node) {
*/
function islandora_group_node_update(NodeInterface $node) {
Utilities::print_log("islandora_group_node_update");
// 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();
if (count($node_terms) ==0) {
Utilities::clear_group_relation_by_entity($node);
}

if (!Utilities::isCalledFromGroupModule()) {
Utilities::adding_islandora_object_to_group($node);

Expand Down Expand Up @@ -149,7 +166,7 @@ function islandora_group_node_update(NodeInterface $node) {
* Implements hook_media_insert().
*/
function islandora_group_media_insert(MediaInterface $media) {
Utilities::print_log("islandora_group_media_insert");
Utilities::drupal_log("islandora_group_media_insert");
// Add this node to group.
if (!Utilities::isCalledFromGroupModule()) {
Utilities::adding_media_only_into_group($media);
Expand All @@ -160,7 +177,7 @@ function islandora_group_media_insert(MediaInterface $media) {
* Implements hook_media_update().
*/
function islandora_group_media_update(MediaInterface $media) {
Utilities::print_log("islandora_group_media_update");
Utilities::drupal_log("islandora_group_media_update");
// Add this node to group.
if (!Utilities::isCalledFromGroupModule()) {
Utilities::adding_media_only_into_group($media);
Expand Down
42 changes: 20 additions & 22 deletions src/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,7 @@ public static function getMedia(NodeInterface $node) {
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public static function clear_group_relation_by_entity($entity) {
// Get access control field from config.
if ($entity->getEntityTypeId() === "node") {
// Get access control field from config.
$access_control_field = self::getAccessControlFieldinNode($entity);
}
elseif ($entity->getEntityTypeId() === "media") {
$access_control_field = self::getAccessControlFieldinMedia($entity);
}

// Check if $access_control_field exists and valid.
if (empty($access_control_field) || !$entity->hasField($access_control_field)) {
return;
}
// For each term, loop through groups-entity.
// Check if group relationships exist for the entity and delete them.
foreach (GroupRelationship::loadByEntity($entity) as $group_content) {
$group_content->delete();
}
Expand Down Expand Up @@ -109,12 +96,12 @@ public static function taggingFieldAccessTermsNode($nid, $targets) {
$node = Node::load($nid);

// Clear any previous relations before adding new ones if called from the UI.
// 1. Clear field_access_terms in media level.
self::untag_existed_field_access_terms($node);

// 2. Clearing group relation with islandora object.
// 1. Clearing group relation with islandora object first.
self::clear_group_relation_by_entity($node);

// 2. Clear field_access_terms.
self::untag_existed_field_access_terms($node);

if (count($targets) > 0) {
// Get access control field from config.
$access_control_field = self::getAccessControlFieldinNode($node);
Expand All @@ -134,6 +121,8 @@ public static function taggingFieldAccessTermsNode($nid, $targets) {
* @return void
*/
public static function taggingFieldAccessTermMedia($media, $targets) {
// Clearing group relation with media first.
self::clear_group_relation_by_entity($media);

self::untag_existed_field_access_terms($media);

Expand Down Expand Up @@ -414,8 +403,16 @@ public static function adding_media_of_islandora_object_to_group($node, $media)
* @return void
*/
public static function clear_term_in_field_access_terms($ne, $group_name) {
// Get access control field from config.
$access_control_field = self::getAccessControlFieldinNode($ne);
// Get access control field from config based on entity type.
if ($ne->getEntityTypeId() === 'node') {
$access_control_field = self::getAccessControlFieldinNode($ne);
}
elseif ($ne->getEntityTypeId() === 'media') {
$access_control_field = self::getAccessControlFieldinMedia($ne);
}
else {
return;
}

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

Expand Down Expand Up @@ -721,7 +719,7 @@ public static function form_submit_delete_relation_untagging_entity_to_group($fo
$form_object = $form_state->getFormObject();
if ($form_object instanceof EntityForm) {
$entity = $form_object->getEntity();
if ($entity->getEntityTypeId() === 'group_content') {
if (in_array($entity->getEntityTypeId(), ['group_relationship', 'group_content'])) {
$group_content = $entity;
$group = $group_content->getGroup();
if ($entity->getEntity()->getEntityTypeId() === "node") {
Expand Down
Loading