View source
<?php
use Drupal\Core\Entity\EntityInterface;
use Drupal\courier\Entity\TemplateCollection;
use Drupal\courier\ChannelInterface;
use Drupal\Core\Entity\ContentEntityInterface;
function courier_entity_insert(EntityInterface $entity) {
_courier_entity_postsave($entity);
}
function courier_entity_update(EntityInterface $entity) {
_courier_entity_postsave($entity);
}
function _courier_entity_postsave(EntityInterface $entity) {
if ($entity instanceof ChannelInterface) {
$template_collection_manager = \Drupal::service('courier.manager.global_template_collection');
$template_collection_manager
->notifyTemplateChanged($entity);
}
}
function courier_entity_load($entities, $entity_type) {
if (!reset($entities) instanceof ChannelInterface) {
return;
}
$template_collection_manager = \Drupal::service('courier.manager.global_template_collection');
foreach ($entities as $entity) {
$template_collection_manager
->importFromGlobalCollection($entity);
}
}
function courier_entity_predelete(EntityInterface $entity) {
if ($entity instanceof ChannelInterface) {
if ($collection = TemplateCollection::getTemplateCollectionForTemplate($entity)) {
$collection
->removeTemplate($entity
->getEntityTypeId())
->save();
}
}
if (!$entity instanceof ContentEntityInterface) {
return;
}
$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));
}
}