View source
<?php
namespace Drupal\Core\Entity;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class EntityTypeListener implements EntityTypeListenerInterface {
protected $entityTypeManager;
protected $entityFieldManager;
protected $eventDispatcher;
protected $entityLastInstalledSchemaRepository;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, EventDispatcherInterface $event_dispatcher, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository) {
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->eventDispatcher = $event_dispatcher;
$this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository;
}
public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
$entity_type_id = $entity_type
->id();
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
if ($storage instanceof EntityTypeListenerInterface) {
$storage
->onEntityTypeCreate($entity_type);
}
$this->entityLastInstalledSchemaRepository
->setLastInstalledDefinition($entity_type);
if ($entity_type
->entityClassImplements(FieldableEntityInterface::class)) {
$this->entityLastInstalledSchemaRepository
->setLastInstalledFieldStorageDefinitions($entity_type_id, $this->entityFieldManager
->getFieldStorageDefinitions($entity_type_id));
}
$this->eventDispatcher
->dispatch(EntityTypeEvents::CREATE, new EntityTypeEvent($entity_type));
$this
->clearCachedDefinitions();
}
public function onFieldableEntityTypeCreate(EntityTypeInterface $entity_type, array $field_storage_definitions) {
$entity_type_id = $entity_type
->id();
$storage = $this->entityTypeManager
->createHandlerInstance($entity_type
->getStorageClass(), $entity_type);
if ($storage instanceof EntityTypeListenerInterface) {
$storage
->onFieldableEntityTypeCreate($entity_type, $field_storage_definitions);
}
$this->entityLastInstalledSchemaRepository
->setLastInstalledDefinition($entity_type);
if ($entity_type
->entityClassImplements(FieldableEntityInterface::class)) {
$this->entityLastInstalledSchemaRepository
->setLastInstalledFieldStorageDefinitions($entity_type_id, $field_storage_definitions);
}
$this->eventDispatcher
->dispatch(EntityTypeEvents::CREATE, new EntityTypeEvent($entity_type));
$this
->clearCachedDefinitions();
}
public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
$storage = $this->entityTypeManager
->createHandlerInstance($entity_type
->getStorageClass(), $entity_type);
if ($storage instanceof EntityTypeListenerInterface) {
$storage
->onEntityTypeUpdate($entity_type, $original);
}
$this->entityLastInstalledSchemaRepository
->setLastInstalledDefinition($entity_type);
$this->eventDispatcher
->dispatch(EntityTypeEvents::UPDATE, new EntityTypeEvent($entity_type, $original));
$this
->clearCachedDefinitions();
}
public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
$entity_type_id = $entity_type
->id();
$storage = $this->entityTypeManager
->createHandlerInstance($entity_type
->getStorageClass(), $entity_type);
if ($storage instanceof EntityTypeListenerInterface) {
$storage
->onEntityTypeDelete($entity_type);
}
$this->entityLastInstalledSchemaRepository
->deleteLastInstalledDefinition($entity_type_id);
$this->eventDispatcher
->dispatch(EntityTypeEvents::DELETE, new EntityTypeEvent($entity_type));
$this
->clearCachedDefinitions();
}
public function onFieldableEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original, array $field_storage_definitions, array $original_field_storage_definitions, array &$sandbox = NULL) {
$entity_type_id = $entity_type
->id();
$storage = $this->entityTypeManager
->createHandlerInstance($entity_type
->getStorageClass(), $entity_type);
if ($storage instanceof EntityTypeListenerInterface) {
$storage
->onFieldableEntityTypeUpdate($entity_type, $original, $field_storage_definitions, $original_field_storage_definitions, $sandbox);
}
if ($sandbox === NULL || isset($sandbox['#finished']) && $sandbox['#finished'] == 1) {
$this->entityLastInstalledSchemaRepository
->setLastInstalledDefinition($entity_type);
if ($entity_type
->entityClassImplements(FieldableEntityInterface::class)) {
$this->entityLastInstalledSchemaRepository
->setLastInstalledFieldStorageDefinitions($entity_type_id, $field_storage_definitions);
}
$this->eventDispatcher
->dispatch(EntityTypeEvents::UPDATE, new EntityTypeEvent($entity_type, $original));
$this
->clearCachedDefinitions();
}
}
protected function clearCachedDefinitions() {
$this->entityTypeManager
->clearCachedDefinitions();
$this->entityFieldManager
->clearCachedFieldDefinitions();
}
}