Skip to content

Commit

Permalink
DDST-382: Subscriber to handle deletion of related Embargoes when a n…
Browse files Browse the repository at this point in the history
…ode is deleted
  • Loading branch information
Prashant-bd committed Jul 29, 2024
1 parent 935129a commit bca65aa
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
16 changes: 16 additions & 0 deletions islandora_entity_status.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
3 changes: 3 additions & 0 deletions islandora_entity_status.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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' ]
42 changes: 42 additions & 0 deletions src/IslandoraNodeDeleteSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Drupal\islandora_entity_status;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeInterface;

class IslandoraNodeDeleteSubscriber {

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

/**
* Constructs a new CustomEntityDeleter object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->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();
}
}
}

0 comments on commit bca65aa

Please sign in to comment.