EntityTestDefinitionSubscriber.php in Drupal 9
File
core/modules/system/tests/modules/entity_test/src/EntityTestDefinitionSubscriber.php
View source
<?php
namespace Drupal\entity_test;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface;
use Drupal\Core\Entity\EntityTypeEvents;
use Drupal\Core\Entity\EntityTypeEventSubscriberTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeListenerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldStorageDefinitionEvents;
use Drupal\Core\Field\FieldStorageDefinitionEventSubscriberTrait;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionListenerInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EntityTestDefinitionSubscriber implements EventSubscriberInterface, EntityTypeListenerInterface, FieldStorageDefinitionListenerInterface {
use EntityTypeEventSubscriberTrait;
use FieldStorageDefinitionEventSubscriberTrait;
protected $state;
protected $entityLastInstalledSchemaRepository;
protected $entityTypeManager;
protected $entityFieldManager;
protected $trackEvents = FALSE;
protected $updateLiveDefinitions = FALSE;
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;
}
public static function getSubscribedEvents() {
return static::getEntityTypeEvents() + static::getFieldStorageDefinitionEvents();
}
public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
if ($this->entityLastInstalledSchemaRepository
->getLastInstalledDefinition($entity_type
->id())) {
$this
->storeDefinitionUpdate(EntityTypeEvents::CREATE);
}
$this
->storeEvent(EntityTypeEvents::CREATE);
if ($this->updateLiveDefinitions) {
$this->entityTypeManager
->getDefinition($entity_type
->id());
$this->state
->set('entity_test_rev.entity_type', $entity_type);
}
}
public function onFieldableEntityTypeCreate(EntityTypeInterface $entity_type, array $field_storage_definitions) {
$this
->storeEvent(EntityTypeEvents::CREATE);
}
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);
if ($this->updateLiveDefinitions) {
$this->entityTypeManager
->getDefinition($entity_type
->id());
$this->state
->set('entity_test_rev.entity_type', $entity_type);
}
}
public function onFieldableEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original, array $field_storage_definitions, array $original_field_storage_definitions, array &$sandbox = NULL) {
$this
->storeEvent(EntityTypeEvents::UPDATE);
}
public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
if (!$this->entityLastInstalledSchemaRepository
->getLastInstalledDefinition($entity_type
->id())) {
$this
->storeDefinitionUpdate(EntityTypeEvents::DELETE);
}
$this
->storeEvent(EntityTypeEvents::DELETE);
if ($this->updateLiveDefinitions) {
$this->entityTypeManager
->getDefinition($entity_type
->id());
$this->state
->set('entity_test_rev.entity_type', '');
}
}
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);
if ($this->updateLiveDefinitions) {
$this->entityFieldManager
->getFieldStorageDefinitions($storage_definition
->getTargetEntityTypeId());
$this->state
->set('entity_test_rev.additional_base_field_definitions', [
$storage_definition
->getName() => $storage_definition,
]);
}
}
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);
if ($this->updateLiveDefinitions) {
$this->entityFieldManager
->getFieldStorageDefinitions($storage_definition
->getTargetEntityTypeId());
$this->state
->set('entity_test_rev.additional_base_field_definitions', [
$storage_definition
->getName() => $storage_definition,
]);
}
}
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);
if ($this->updateLiveDefinitions) {
$this->entityFieldManager
->getFieldStorageDefinitions($storage_definition
->getTargetEntityTypeId());
$this->state
->set('entity_test_rev.additional_base_field_definitions', []);
}
}
public function enableEventTracking() {
$this->trackEvents = TRUE;
}
public function enableLiveDefinitionUpdates() {
$this->updateLiveDefinitions = TRUE;
}
public function hasEventFired($event_name) {
return (bool) $this->state
->get($event_name);
}
protected function storeEvent($event_name) {
if ($this->trackEvents) {
$this->state
->set($event_name, TRUE);
}
}
public function hasDefinitionBeenUpdated($event_name) {
return (bool) $this->state
->get($event_name . '_updated_definition');
}
protected function storeDefinitionUpdate($event_name) {
if ($this->trackEvents) {
$this->state
->set($event_name . '_updated_definition', TRUE);
}
}
}