You are here

function entityqueue_entity_delete in Entityqueue 8

Implements hook_entity_delete().

@todo Remove this when https://www.drupal.org/node/2723323 is fixed.

File

./entityqueue.module, line 90
Allows users to collect entities in arbitrarily ordered lists.

Code

function entityqueue_entity_delete(EntityInterface $entity) {

  // Get all the entity queues referencing the targets entity type.
  $queues = EntityQueue::loadMultipleByTargetType($entity
    ->getEntityTypeId());
  foreach ($queues as $queue) {

    // Get all the subqueues which are referencing the deleted entity.
    $query = \Drupal::entityQuery('entity_subqueue')
      ->condition('queue', $queue
      ->id())
      ->condition('items', $entity
      ->id());
    $result = $query
      ->execute();
    $subqueues = EntitySubqueue::loadMultiple($result);

    // Check if the entity is referenced in a subqueue.
    foreach ($subqueues as $subqueue) {
      $items = $subqueue
        ->get('items')
        ->getValue();
      if (($item_key = array_search($entity
        ->id(), array_column($items, 'target_id'))) !== FALSE) {
        unset($items[$item_key]);
        $subqueue
          ->set('items', $items);
        $subqueue
          ->save();
      }
    }
  }
}