You are here

function courier_entity_predelete in Courier 8

Same name and namespace in other branches
  1. 2.x courier.module \courier_entity_predelete()

Implements hook_entity_predelete().

File

./courier.module, line 56

Code

function courier_entity_predelete(EntityInterface $entity) {

  // Remove references from TemplateCollection when a Channel entity is deleted.
  if ($entity instanceof ChannelInterface) {
    if ($collection = TemplateCollection::getTemplateCollectionForTemplate($entity)) {
      $collection
        ->removeTemplate($entity
        ->getEntityTypeId())
        ->save();
    }
  }

  // Postgres has strict type checks.
  // See https://github.com/dpi/rng/issues/50
  if (!$entity instanceof ContentEntityInterface) {
    return;
  }

  // Remove template collections if owner entity is deleted.
  $template_collection_storage = \Drupal::entityManager()
    ->getStorage('courier_template_collection');
  $query = $template_collection_storage
    ->getQuery();
  $group = $query
    ->andConditionGroup()
    ->condition('owner__target_type', $entity
    ->getEntityTypeId(), '=')
    ->condition('owner__target_id', $entity
    ->id(), '=');
  $query
    ->condition($group);
  if ($ids = $query
    ->execute()) {
    $template_collection_storage
      ->delete($template_collection_storage
      ->loadMultiple($ids));
  }
}