class EntityTypeListener

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Entity/EntityTypeListener.php \Drupal\Core\Entity\EntityTypeListener
  2. 10 core/lib/Drupal/Core/Entity/EntityTypeListener.php \Drupal\Core\Entity\EntityTypeListener
  3. 11.x core/lib/Drupal/Core/Entity/EntityTypeListener.php \Drupal\Core\Entity\EntityTypeListener

Reacts to entity type CRUD on behalf of the Entity system.

Hierarchy

Expanded class hierarchy of EntityTypeListener

See also

\Drupal\Core\Entity\EntityTypeEvents

1 string reference to 'EntityTypeListener'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses EntityTypeListener
entity_type.listener in core/core.services.yml
Drupal\Core\Entity\EntityTypeListener

File

core/lib/Drupal/Core/Entity/EntityTypeListener.php, line 12

Namespace

Drupal\Core\Entity
View source
class EntityTypeListener implements EntityTypeListenerInterface {
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * The entity field manager.
     *
     * @var \Drupal\Core\Entity\EntityFieldManagerInterface
     */
    protected $entityFieldManager;
    
    /**
     * The event dispatcher.
     *
     * @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface
     */
    protected $eventDispatcher;
    
    /**
     * The entity last installed schema repository.
     *
     * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
     */
    protected $entityLastInstalledSchemaRepository;
    
    /**
     * Constructs a new EntityTypeListener.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager.
     * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
     *   The entity field manager.
     * @param \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $event_dispatcher
     *   The event dispatcher.
     * @param \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository
     *   The entity last installed schema repository.
     */
    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;
    }
    
    /**
     * {@inheritdoc}
     */
    public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
        $entity_type_id = $entity_type->id();
        // @todo Forward this to all interested handlers, not only storage, once
        //   iterating handlers is possible: https://www.drupal.org/node/2332857.
        $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(new EntityTypeEvent($entity_type), EntityTypeEvents::CREATE);
        $this->clearCachedDefinitions();
    }
    
    /**
     * {@inheritdoc}
     */
    public function onFieldableEntityTypeCreate(EntityTypeInterface $entity_type, array $field_storage_definitions) {
        $entity_type_id = $entity_type->id();
        // @todo Forward this to all interested handlers, not only storage, once
        //   iterating handlers is possible: https://www.drupal.org/node/2332857.
        $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(new EntityTypeEvent($entity_type), EntityTypeEvents::CREATE);
        $this->clearCachedDefinitions();
    }
    
    /**
     * {@inheritdoc}
     */
    public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
        // An entity type can be updated even when its live (in-code) definition has
        // been removed from the codebase, so we need to instantiate a custom
        // storage handler that uses the passed-in entity type definition.
        $storage = $this->entityTypeManager
            ->createHandlerInstance($entity_type->getStorageClass(), $entity_type);
        // @todo Forward this to all interested handlers, not only storage, once
        //   iterating handlers is possible: https://www.drupal.org/node/2332857.
        if ($storage instanceof EntityTypeListenerInterface) {
            $storage->onEntityTypeUpdate($entity_type, $original);
        }
        $this->entityLastInstalledSchemaRepository
            ->setLastInstalledDefinition($entity_type);
        $this->eventDispatcher
            ->dispatch(new EntityTypeEvent($entity_type, $original), EntityTypeEvents::UPDATE);
        $this->clearCachedDefinitions();
    }
    
    /**
     * {@inheritdoc}
     */
    public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
        $entity_type_id = $entity_type->id();
        // An entity type can be deleted even when its live (in-code) definition has
        // been removed from the codebase, so we need to instantiate a custom
        // storage handler that uses the passed-in entity type definition.
        $storage = $this->entityTypeManager
            ->createHandlerInstance($entity_type->getStorageClass(), $entity_type);
        // @todo Forward this to all interested handlers, not only storage, once
        //   iterating handlers is possible: https://www.drupal.org/node/2332857.
        if ($storage instanceof EntityTypeListenerInterface) {
            $storage->onEntityTypeDelete($entity_type);
        }
        $this->entityLastInstalledSchemaRepository
            ->deleteLastInstalledDefinition($entity_type_id);
        $this->eventDispatcher
            ->dispatch(new EntityTypeEvent($entity_type), EntityTypeEvents::DELETE);
        $this->clearCachedDefinitions();
    }
    
    /**
     * {@inheritdoc}
     */
    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();
        // @todo Forward this to all interested handlers, not only storage, once
        //   iterating handlers is possible: https://www.drupal.org/node/2332857.
        $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(new EntityTypeEvent($entity_type, $original), EntityTypeEvents::UPDATE);
            $this->clearCachedDefinitions();
        }
    }
    
    /**
     * Clears necessary caches to apply entity/field definition updates.
     */
    protected function clearCachedDefinitions() {
        $this->entityTypeManager
            ->clearCachedDefinitions();
        $this->entityFieldManager
            ->clearCachedFieldDefinitions();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
EntityTypeListener::$entityFieldManager protected property The entity field manager.
EntityTypeListener::$entityLastInstalledSchemaRepository protected property The entity last installed schema repository.
EntityTypeListener::$entityTypeManager protected property The entity type manager.
EntityTypeListener::$eventDispatcher protected property The event dispatcher.
EntityTypeListener::clearCachedDefinitions protected function Clears necessary caches to apply entity/field definition updates.
EntityTypeListener::onEntityTypeCreate public function Reacts to the creation of the entity type. Overrides EntityTypeListenerInterface::onEntityTypeCreate
EntityTypeListener::onEntityTypeDelete public function Reacts to the deletion of the entity type. Overrides EntityTypeListenerInterface::onEntityTypeDelete
EntityTypeListener::onEntityTypeUpdate public function Reacts to the update of the entity type. Overrides EntityTypeListenerInterface::onEntityTypeUpdate
EntityTypeListener::onFieldableEntityTypeCreate public function Reacts to the creation of the fieldable entity type. Overrides EntityTypeListenerInterface::onFieldableEntityTypeCreate
EntityTypeListener::onFieldableEntityTypeUpdate public function Reacts to the update of a fieldable entity type. Overrides EntityTypeListenerInterface::onFieldableEntityTypeUpdate
EntityTypeListener::__construct public function Constructs a new EntityTypeListener.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.