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

BCIR-246 : Refactor islandora_entity_status - Events #7

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

Cascades entity status to all referenced entities in an Islandora repository item

## Usage
- Node Update
- On status change update all media and assign same status.
- If node is collection, on status change, update status of all attached nodes.
- Media presave
- Assign the status of the Media Of node.

## Requirements

This module requires the following modules/libraries:
Expand Down
1 change: 1 addition & 0 deletions islandora_entity_status.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ dependencies:
- drupal:node
- drupal:file
- islandora:islandora
- islandora_events:islandora_events
141 changes: 0 additions & 141 deletions islandora_entity_status.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,7 @@
* Hook implementations.
*/

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\islandora\IslandoraUtils;
use Drupal\media\Entity\Media;
use Drupal\media\MediaInterface;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Twig\Markup;

/**
* 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_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();
}

// Trigger the batch process for collection node.
$node_ids_to_update = find_collection_nodes($nid);
$latestStatus = $entity->get('status')->value;

islandora_entity_status_trigger_batch_process($node_ids_to_update, $latestStatus);
}
}

/**
* Implements hook_form_FORM_ID_alter().
Expand All @@ -74,91 +21,3 @@ function islandora_entity_status_form_node_islandora_object_edit_form_alter(&$fo
// Pass the confirmation message to JavaScript.
$form['#attached']['drupalSettings']['custom_confirm_popup']['message'] = $message;
}

/**
* Find related nodes.
*/
function find_collection_nodes($currentNodeId) {
$relatedNodeIds = [];

// Initial query to find nodes where the field_member_of contains
// the current node ID.
$query = \Drupal::entityQuery('node')
->condition('type', 'islandora_object')
->condition('field_member_of', $currentNodeId, '=')
->accessCheck(FALSE);

$result = $query->execute();

$relatedNodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($result);

if (!empty($relatedNodes)) {
foreach ($relatedNodes as $relatedNode) {
$relatedNodeIds[] = $relatedNode->id();
}
}

return $relatedNodeIds;
}

/**
* Helper function to trigger the batch process.
*/
function islandora_entity_status_trigger_batch_process($node_ids, $node_status) {
// Create a batch operation.
$operations = [
['islandora_entity_status_batch_operation', [$node_ids, $node_status]],
];

// Create a batch.
$batch = [
'title' => t('Processing nodes'),
'operations' => $operations,
'finished' => 'islandora_entity_status_batch_finished',
];

// Add the batch to the queue.
batch_set($batch);
}

/**
* Batch operation callback.
*/
function islandora_entity_status_batch_operation($node_ids, $node_status, &$context) {
// Perform your batch processing here.
// Update the status for each related node.
foreach ($node_ids as $relatedNodeId) {
$relatedNode = Node::load($relatedNodeId);
$relatedNode->set('status', $node_status);
$relatedNode->save();

// Update the progress.
$context['results'][] = t('Node %node processed and status set to %status.',
['%node' => $relatedNodeId, '%status' => $node_status]);
}
}

/**
* Batch finished callback.
*/
function islandora_entity_status_batch_finished($success, $results, $operations) {
$messenger = \Drupal::messenger();
$message = '';

if ($success) {
if (!empty($results)) {
// Batch processing completed successfully.
// Display a message indicating success.
$messenger->addMessage(t('Batch processing completed successfully.'));

foreach ($results as $result) {
$message .= '<br>' . $result;
}
$messenger->addMessage(new Markup($message, 'html'));
}
}
else {
// Batch processing failed.
$messenger->addError(t('Batch processing failed.'));
}
}
11 changes: 11 additions & 0 deletions islandora_entity_status.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@ services:
- '@entity_type.manager'
tags:
- { name: drush.command }

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

islandora_entity_status.islandora_media_entity_subscriber:
class: Drupal\islandora_entity_status\EventSubscriber\IslandoraMediaEventSubscriber
tags:
- { name: event_subscriber }
37 changes: 37 additions & 0 deletions src/EventSubscriber/IslandoraMediaEventSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Drupal\islandora_entity_status\EventSubscriber;

use Drupal\islandora_events\Event\IslandoraMediaEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

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

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
IslandoraMediaEvent::PRE_SAVE => 'onIslandoraMediaPresave',
];
}

/**
* Reacts to the Islandora Media presave event.
*
* @param \Drupal\islandora_events\Event\IslandoraMediaEvent $event
* The Islandora Media event.
*/
public function onIslandoraMediaPresave(IslandoraMediaEvent $event) {
$media = $event->getMedia();
$media_of_node = $event->getReferencedNode();

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

}
Loading
Loading