From bca65aa508aa60fc84190254e53229303b1d645a Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Mon, 29 Jul 2024 14:03:22 -0400 Subject: [PATCH] DDST-382: Subscriber to handle deletion of related Embargoes when a node is deleted --- islandora_entity_status.module | 16 ++++++++++ islandora_entity_status.services.yml | 3 ++ src/IslandoraNodeDeleteSubscriber.php | 42 +++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 src/IslandoraNodeDeleteSubscriber.php diff --git a/islandora_entity_status.module b/islandora_entity_status.module index 2433a89..06981c7 100644 --- a/islandora_entity_status.module +++ b/islandora_entity_status.module @@ -5,6 +5,7 @@ * Hook implementations. */ +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\islandora\IslandoraUtils; @@ -165,3 +166,18 @@ function islandora_entity_status_batch_finished($success, $results, $operations) $messenger->addError(t('Batch processing failed.')); } } + + +/** + * Implements hook_entity_delete(). + */ +function islandora_entity_status_entity_delete(EntityInterface $entity) { + if (!$entity instanceof ContentEntityInterface) { + return; + } + + // Check if the entity is a node with the bundle "islandora_object". + if ($entity->hasField(IslandoraUtils::MEMBER_OF_FIELD)) { + \Drupal::service('islandora_entity_status.islandora_node_delete_subscriber')->deleteAssociatedCustomEntity($entity); + } +} diff --git a/islandora_entity_status.services.yml b/islandora_entity_status.services.yml index 78543d4..3a883d4 100644 --- a/islandora_entity_status.services.yml +++ b/islandora_entity_status.services.yml @@ -5,3 +5,6 @@ services: - '@entity_type.manager' tags: - { name: drush.command } + islandora_entity_status.islandora_node_delete_subscriber: + class: Drupal\islandora_entity_status\IslandoraNodeDeleteSubscriber + arguments: [ '@entity_type.manager' ] diff --git a/src/IslandoraNodeDeleteSubscriber.php b/src/IslandoraNodeDeleteSubscriber.php new file mode 100644 index 0000000..2fb9785 --- /dev/null +++ b/src/IslandoraNodeDeleteSubscriber.php @@ -0,0 +1,42 @@ +entityTypeManager = $entity_type_manager; + } + + /** + * Deletes the associated custom entity. + * + * @param \Drupal\node\NodeInterface $node + * The Islandora object node being deleted. + */ + public function deleteAssociatedCustomEntity(NodeInterface $node): void { + // Replace 'your_custom_entity_type' with the actual machine name of your custom entity type. + $custom_entity_storage = $this->entityTypeManager->getStorage('embargo'); + $custom_entities = $custom_entity_storage->loadByProperties(['embargoed_node' => $node->id()]); + + foreach ($custom_entities as $custom_entity) { + $custom_entity->delete(); + } + } +}