diff --git a/islandora_entity_status.module b/islandora_entity_status.module new file mode 100644 index 0000000..cdd6a53 --- /dev/null +++ b/islandora_entity_status.module @@ -0,0 +1,51 @@ +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(); + } + } +}