class EntityTestDefinitionSubscriber

Same name in other branches
  1. 8.9.x core/modules/system/tests/modules/entity_test/src/EntityTestDefinitionSubscriber.php \Drupal\entity_test\EntityTestDefinitionSubscriber
  2. 10 core/modules/system/tests/modules/entity_test/src/EntityTestDefinitionSubscriber.php \Drupal\entity_test\EntityTestDefinitionSubscriber
  3. 11.x core/modules/system/tests/modules/entity_test/src/EntityTestDefinitionSubscriber.php \Drupal\entity_test\EntityTestDefinitionSubscriber

Test entity type and field storage definition event subscriber.

Hierarchy

Expanded class hierarchy of EntityTestDefinitionSubscriber

1 string reference to 'EntityTestDefinitionSubscriber'
entity_test.services.yml in core/modules/system/tests/modules/entity_test/entity_test.services.yml
core/modules/system/tests/modules/entity_test/entity_test.services.yml
1 service uses EntityTestDefinitionSubscriber
entity_test.definition.subscriber in core/modules/system/tests/modules/entity_test/entity_test.services.yml
Drupal\entity_test\EntityTestDefinitionSubscriber

File

core/modules/system/tests/modules/entity_test/src/EntityTestDefinitionSubscriber.php, line 22

Namespace

Drupal\entity_test
View source
class EntityTestDefinitionSubscriber implements EventSubscriberInterface, EntityTypeListenerInterface, FieldStorageDefinitionListenerInterface {
    use EntityTypeEventSubscriberTrait;
    use FieldStorageDefinitionEventSubscriberTrait;
    
    /**
     * The state service.
     *
     * @var \Drupal\Core\State\StateInterface
     */
    protected $state;
    
    /**
     * The last installed schema repository.
     *
     * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
     */
    protected $entityLastInstalledSchemaRepository;
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * The entity field manager.
     *
     * @var \Drupal\Core\Entity\EntityFieldManagerInterface
     */
    protected $entityFieldManager;
    
    /**
     * Flag determining whether events should be tracked.
     *
     * @var bool
     */
    protected $trackEvents = FALSE;
    
    /**
     * Determines whether the live definitions should be updated.
     *
     * @var bool
     */
    protected $updateLiveDefinitions = FALSE;
    
    /**
     * {@inheritdoc}
     */
    public function __construct(StateInterface $state, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager) {
        $this->state = $state;
        $this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository;
        $this->entityTypeManager = $entity_type_manager;
        $this->entityFieldManager = $entity_field_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() {
        return static::getEntityTypeEvents() + static::getFieldStorageDefinitionEvents();
    }
    
    /**
     * {@inheritdoc}
     */
    public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
        if ($this->entityLastInstalledSchemaRepository
            ->getLastInstalledDefinition($entity_type->id())) {
            $this->storeDefinitionUpdate(EntityTypeEvents::CREATE);
        }
        $this->storeEvent(EntityTypeEvents::CREATE);
        // Retrieve the live entity type definition in order to warm the static
        // cache and then insert the new entity type definition, so we can test that
        // the cache doesn't get stale after the event has fired.
        if ($this->updateLiveDefinitions) {
            $this->entityTypeManager
                ->getDefinition($entity_type->id());
            $this->state
                ->set('entity_test_rev.entity_type', $entity_type);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function onFieldableEntityTypeCreate(EntityTypeInterface $entity_type, array $field_storage_definitions) {
        $this->storeEvent(EntityTypeEvents::CREATE);
    }
    
    /**
     * {@inheritdoc}
     */
    public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
        $last_installed_definition = $this->entityLastInstalledSchemaRepository
            ->getLastInstalledDefinition($entity_type->id());
        if ((string) $last_installed_definition->getLabel() === 'Updated entity test rev') {
            $this->storeDefinitionUpdate(EntityTypeEvents::UPDATE);
        }
        $this->storeEvent(EntityTypeEvents::UPDATE);
        // Retrieve the live entity type definition in order to warm the static
        // cache and then insert the new entity type definition, so we can test that
        // the cache doesn't get stale after the event has fired.
        if ($this->updateLiveDefinitions) {
            $this->entityTypeManager
                ->getDefinition($entity_type->id());
            $this->state
                ->set('entity_test_rev.entity_type', $entity_type);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function onFieldableEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original, array $field_storage_definitions, array $original_field_storage_definitions, array &$sandbox = NULL) {
        $this->storeEvent(EntityTypeEvents::UPDATE);
    }
    
    /**
     * {@inheritdoc}
     */
    public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
        if (!$this->entityLastInstalledSchemaRepository
            ->getLastInstalledDefinition($entity_type->id())) {
            $this->storeDefinitionUpdate(EntityTypeEvents::DELETE);
        }
        $this->storeEvent(EntityTypeEvents::DELETE);
        // Retrieve the live entity type definition in order to warm the static
        // cache and then delete the new entity type definition, so we can test that
        // the cache doesn't get stale after the event has fired.
        if ($this->updateLiveDefinitions) {
            $this->entityTypeManager
                ->getDefinition($entity_type->id());
            $this->state
                ->set('entity_test_rev.entity_type', '');
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition) {
        if (isset($this->entityLastInstalledSchemaRepository
            ->getLastInstalledFieldStorageDefinitions($storage_definition->getTargetEntityTypeId())[$storage_definition->getName()])) {
            $this->storeDefinitionUpdate(FieldStorageDefinitionEvents::CREATE);
        }
        $this->storeEvent(FieldStorageDefinitionEvents::CREATE);
        // Retrieve the live field storage definitions in order to warm the static
        // cache and then insert the new storage definition, so we can test that the
        // cache doesn't get stale after the event has fired.
        if ($this->updateLiveDefinitions) {
            $this->entityFieldManager
                ->getFieldStorageDefinitions($storage_definition->getTargetEntityTypeId());
            $this->state
                ->set('entity_test_rev.additional_base_field_definitions', [
                $storage_definition->getName() => $storage_definition,
            ]);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
        $last_installed_definition = $this->entityLastInstalledSchemaRepository
            ->getLastInstalledFieldStorageDefinitions($storage_definition->getTargetEntityTypeId())[$storage_definition->getName()];
        if ((string) $last_installed_definition->getLabel() === 'Updated field storage test') {
            $this->storeDefinitionUpdate(FieldStorageDefinitionEvents::UPDATE);
        }
        $this->storeEvent(FieldStorageDefinitionEvents::UPDATE);
        // Retrieve the live field storage definitions in order to warm the static
        // cache and then insert the new storage definition, so we can test that the
        // cache doesn't get stale after the event has fired.
        if ($this->updateLiveDefinitions) {
            $this->entityFieldManager
                ->getFieldStorageDefinitions($storage_definition->getTargetEntityTypeId());
            $this->state
                ->set('entity_test_rev.additional_base_field_definitions', [
                $storage_definition->getName() => $storage_definition,
            ]);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $storage_definition) {
        if (!isset($this->entityLastInstalledSchemaRepository
            ->getLastInstalledFieldStorageDefinitions($storage_definition->getTargetEntityTypeId())[$storage_definition->getName()])) {
            $this->storeDefinitionUpdate(FieldStorageDefinitionEvents::DELETE);
        }
        $this->storeEvent(FieldStorageDefinitionEvents::DELETE);
        // Retrieve the live field storage definitions in order to warm the static
        // cache and then remove the new storage definition, so we can test that the
        // cache doesn't get stale after the event has fired.
        if ($this->updateLiveDefinitions) {
            $this->entityFieldManager
                ->getFieldStorageDefinitions($storage_definition->getTargetEntityTypeId());
            $this->state
                ->set('entity_test_rev.additional_base_field_definitions', []);
        }
    }
    
    /**
     * Enables event tracking.
     */
    public function enableEventTracking() {
        $this->trackEvents = TRUE;
    }
    
    /**
     * Enables live definition updates.
     */
    public function enableLiveDefinitionUpdates() {
        $this->updateLiveDefinitions = TRUE;
    }
    
    /**
     * Checks whether an event has been dispatched.
     *
     * @param string $event_name
     *   The event name.
     *
     * @return bool
     *   TRUE if the event has been dispatched, FALSE otherwise.
     */
    public function hasEventFired($event_name) {
        return (bool) $this->state
            ->get($event_name);
    }
    
    /**
     * Stores the specified event.
     *
     * @param string $event_name
     *   The event name.
     */
    protected function storeEvent($event_name) {
        if ($this->trackEvents) {
            $this->state
                ->set($event_name, TRUE);
        }
    }
    
    /**
     * Checks whether the installed definitions were updated before the event.
     *
     * @param string $event_name
     *   The event name.
     *
     * @return bool
     *   TRUE if the last installed entity type of field storage definitions have
     *   been updated before the event was fired, FALSE otherwise.
     */
    public function hasDefinitionBeenUpdated($event_name) {
        return (bool) $this->state
            ->get($event_name . '_updated_definition');
    }
    
    /**
     * Stores the installed definition state for the specified event.
     *
     * @param string $event_name
     *   The event name.
     */
    protected function storeDefinitionUpdate($event_name) {
        if ($this->trackEvents) {
            $this->state
                ->set($event_name . '_updated_definition', TRUE);
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
EntityTestDefinitionSubscriber::$entityFieldManager protected property The entity field manager.
EntityTestDefinitionSubscriber::$entityLastInstalledSchemaRepository protected property The last installed schema repository.
EntityTestDefinitionSubscriber::$entityTypeManager protected property The entity type manager.
EntityTestDefinitionSubscriber::$state protected property The state service.
EntityTestDefinitionSubscriber::$trackEvents protected property Flag determining whether events should be tracked.
EntityTestDefinitionSubscriber::$updateLiveDefinitions protected property Determines whether the live definitions should be updated.
EntityTestDefinitionSubscriber::enableEventTracking public function Enables event tracking.
EntityTestDefinitionSubscriber::enableLiveDefinitionUpdates public function Enables live definition updates.
EntityTestDefinitionSubscriber::getSubscribedEvents public static function
EntityTestDefinitionSubscriber::hasDefinitionBeenUpdated public function Checks whether the installed definitions were updated before the event.
EntityTestDefinitionSubscriber::hasEventFired public function Checks whether an event has been dispatched.
EntityTestDefinitionSubscriber::onEntityTypeCreate public function Reacts to the creation of the entity type. Overrides EntityTypeEventSubscriberTrait::onEntityTypeCreate
EntityTestDefinitionSubscriber::onEntityTypeDelete public function Reacts to the deletion of the entity type. Overrides EntityTypeEventSubscriberTrait::onEntityTypeDelete
EntityTestDefinitionSubscriber::onEntityTypeUpdate public function Reacts to the update of the entity type. Overrides EntityTypeEventSubscriberTrait::onEntityTypeUpdate
EntityTestDefinitionSubscriber::onFieldableEntityTypeCreate public function Reacts to the creation of the fieldable entity type. Overrides EntityTypeEventSubscriberTrait::onFieldableEntityTypeCreate
EntityTestDefinitionSubscriber::onFieldableEntityTypeUpdate public function Reacts to the update of a fieldable entity type. Overrides EntityTypeEventSubscriberTrait::onFieldableEntityTypeUpdate
EntityTestDefinitionSubscriber::onFieldStorageDefinitionCreate public function Reacts to the creation of a field storage definition. Overrides FieldStorageDefinitionEventSubscriberTrait::onFieldStorageDefinitionCreate
EntityTestDefinitionSubscriber::onFieldStorageDefinitionDelete public function Reacts to the deletion of a field storage definition. Overrides FieldStorageDefinitionEventSubscriberTrait::onFieldStorageDefinitionDelete
EntityTestDefinitionSubscriber::onFieldStorageDefinitionUpdate public function Reacts to the update of a field storage definition. Overrides FieldStorageDefinitionEventSubscriberTrait::onFieldStorageDefinitionUpdate
EntityTestDefinitionSubscriber::storeDefinitionUpdate protected function Stores the installed definition state for the specified event.
EntityTestDefinitionSubscriber::storeEvent protected function Stores the specified event.
EntityTestDefinitionSubscriber::__construct public function
EntityTypeEventSubscriberTrait::getEntityTypeEvents public static function Gets the subscribed events.
EntityTypeEventSubscriberTrait::onEntityTypeEvent public function Listener method for any entity type definition event.
FieldStorageDefinitionEventSubscriberTrait::getFieldStorageDefinitionEvents public static function Returns the subscribed events.
FieldStorageDefinitionEventSubscriberTrait::onFieldStorageDefinitionEvent public function Listener method for any field storage definition event.

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