Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DGIR-44 : Update hook for cascading Entity Status #3

Merged
merged 6 commits into from
Nov 24, 2023
Merged
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
26 changes: 26 additions & 0 deletions islandora_entity_status.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
* Hook implementations.
*/

use Drupal\Core\Entity\EntityInterface;
use Drupal\islandora\IslandoraUtils;
use Drupal\media\MediaInterface;
use Drupal\node\NodeInterface;
use Drupal\media\Entity\Media;

/**
* Implements hook_ENTITY_TYPE_presave().
Expand All @@ -24,3 +26,27 @@ function islandora_entity_status_media_presave(MediaInterface $media) {
}
}
}

/**
* Implements hook_ENTITY_TYPE_update().
*/
function islandora_entity_status_node_update(EntityInterface $entity) {
// Check if the entity is a node with the bundle "islandora_object".
if ($entity->hasField(IslandoraUtils::MEMBER_OF_FIELD)) {
// Get the current node ID.
$nid = $entity->id();

// Query for media items that are associated with the current node.
$query = \Drupal::entityQuery('media')
->accessCheck(FALSE)
->condition(IslandoraUtils::MEDIA_OF_FIELD, $nid);
$media_ids = $query->execute();

// Load the media items and set their status to the same status as the node.
$media_items = Media::loadMultiple($media_ids);
foreach ($media_items as $media_item) {
$media_item->set('status', $entity->get('status')->value);
$media_item->save();
}
}
}
Loading