-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DGIR-44 : Update hook for cascading Entity Status
- Loading branch information
1 parent
076fc08
commit ea419c4
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |