Skip to content

Commit

Permalink
BCIR-246: Node update event move from hooks to events subscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashant-bd committed Feb 13, 2024
1 parent 3be224b commit 9a99494
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 31 deletions.
15 changes: 0 additions & 15 deletions islandora_entity_status.module
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,7 @@ use Twig\Markup;
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();
}

// Trigger the batch process for collection node.
$node_ids_to_update = find_collection_nodes($nid);
$latestStatus = $entity->get('status')->value;
Expand Down
8 changes: 7 additions & 1 deletion islandora_entity_status.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ services:
tags:
- { name: drush.command }

islandora_entity_status.islandora_node_entity_subscriber:
class: Drupal\islandora_entity_status\EventSubscriber\IslandoraNodeEntitySubscriber
arguments: ['@current_user', '@entity_type.manager']
tags:
- { name: event_subscriber }

islandora_entity_status.islandora_media_entity_subscriber:
class: Drupal\islandora_entity_status\EventSubscriber\IslandoraMediaEntitySubscriber
arguments: ['@current_user', '@logger.factory']
arguments: ['@current_user']
tags:
- { name: event_subscriber }
19 changes: 4 additions & 15 deletions src/EventSubscriber/IslandoraMediaEntitySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Drupal\islandora_entity_status\EventSubscriber;

use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\islandora_events\Event\IslandoraMediaEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Session\AccountProxyInterface;
Expand All @@ -19,24 +18,14 @@ class IslandoraMediaEntitySubscriber implements EventSubscriberInterface {
*/
protected $currentUser;

/**
* The logger factory.
*
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;

/**
* Constructs a new IslandoraMediaSubscriber object.
*
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* The current user.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger factory.
*/
public function __construct(AccountProxyInterface $current_user, LoggerChannelFactoryInterface $logger_factory) {
public function __construct(AccountProxyInterface $current_user) {
$this->currentUser = $current_user;
$this->loggerFactory = $logger_factory;
}

/**
Expand All @@ -56,11 +45,11 @@ public static function getSubscribedEvents() {
*/
public function onIslandoraMediaPresave(IslandoraMediaEvent $event) {
$media = $event->getMedia();
$referenced_node = $event->getReferencedNode();
$media_of_node = $event->getReferencedNode();

// Set media status same as media_of node status.
$referenced_node_status = intval($referenced_node->status->value);
$media->set('status', $referenced_node_status);
$media_of_node_status = intval($media_of_node->status->value);
$media->set('status', $media_of_node_status);
}

}
80 changes: 80 additions & 0 deletions src/EventSubscriber/IslandoraNodeEntitySubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Drupal\islandora_entity_status\EventSubscriber;

use Drupal\islandora\IslandoraUtils;
use Drupal\islandora_events\Event\IslandoraNodeEvent;
use Drupal\media\Entity\Media;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;

/**
* Defines an event subscriber for Islandora Media.
*/
class IslandoraNodeEntitySubscriber implements EventSubscriberInterface {

/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* Constructs a new IslandoraMediaSubscriber object.
*
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* The current user.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(AccountProxyInterface $current_user, EntityTypeManagerInterface $entity_type_manager) {
$this->currentUser = $current_user;
$this->entityTypeManager = $entity_type_manager;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
IslandoraNodeEvent::UPDATE => 'onIslandoraNodeUpdated',
];
}

/**
* Reacts to node update events.
*
* @param \Drupal\islandora_events\Event\IslandoraNodeEvent $event
* The Islandora node event.
*/
public function onIslandoraNodeUpdated(IslandoraNodeEvent $event) {
// Get the current node ID.
$node = $event->getNode();
$nid = $node->id();

// Query for media items that are associated with the current node.
$query = $this->entityTypeManager->getStorage('media')->getQuery();
$query->accessCheck(FALSE);
$query->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 = $this->entityTypeManager->getStorage('media')->loadMultiple($media_ids);
foreach ($media_items as $media_item) {
// We just need to save the media.
// Status is getting handled in pre_save.
// @see IslandoraMediaEntitySubscriber:onIslandoraMediaPresave.
$media_item->save();
}
}

}

0 comments on commit 9a99494

Please sign in to comment.