Skip to content

Commit

Permalink
DGIR-44 : Update hook for cascading Entity Status
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashant-bd committed Nov 16, 2023
1 parent 076fc08 commit ea419c4
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions islandora_entity_status.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* @file
* Hook implementations.
*/

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

/**
* Implements hook_ENTITY_TYPE_presave().
*/
function islandora_entity_status_media_presave(MediaInterface $media) {
if ($media->hasField(IslandoraUtils::MEDIA_OF_FIELD)) {
$media_of = $media->get(IslandoraUtils::MEDIA_OF_FIELD);
if (!$media_of->isEmpty()) {
$node = $media_of->referencedEntities()[0];
if ($node instanceof NodeInterface) {
$node_status = intval($node->status->value);
$media->set('status', $node_status);
}
}
}
}

/**
* Implements hook_entity_update().
*/
function islandora_entity_status_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
// Check if the entity is a node with the bundle "islandora_object".
if ($entity->getEntityTypeId() == 'node' && $entity->bundle() == 'islandora_object') {
// 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();
}
}
}

0 comments on commit ea419c4

Please sign in to comment.