FieldStorageDefinitionListener.php in Drupal 8
File
core/lib/Drupal/Core/Field/FieldStorageDefinitionListener.php
View source
<?php
namespace Drupal\Core\Field;
use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityStorageInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class FieldStorageDefinitionListener implements FieldStorageDefinitionListenerInterface {
protected $entityTypeManager;
protected $eventDispatcher;
protected $entityLastInstalledSchemaRepository;
protected $entityFieldManager;
protected $deletedFieldsRepository;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository, EntityFieldManagerInterface $entity_field_manager, DeletedFieldsRepositoryInterface $deleted_fields_repository) {
$this->entityTypeManager = $entity_type_manager;
$this->eventDispatcher = $event_dispatcher;
$this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository;
$this->entityFieldManager = $entity_field_manager;
$this->deletedFieldsRepository = $deleted_fields_repository;
}
public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition) {
$entity_type_id = $storage_definition
->getTargetEntityTypeId();
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
if ($storage instanceof FieldStorageDefinitionListenerInterface) {
$storage
->onFieldStorageDefinitionCreate($storage_definition);
}
$this->entityLastInstalledSchemaRepository
->setLastInstalledFieldStorageDefinition($storage_definition);
$this->eventDispatcher
->dispatch(FieldStorageDefinitionEvents::CREATE, new FieldStorageDefinitionEvent($storage_definition));
$this->entityFieldManager
->clearCachedFieldDefinitions();
}
public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
$entity_type_id = $storage_definition
->getTargetEntityTypeId();
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
if ($storage instanceof FieldStorageDefinitionListenerInterface) {
$storage
->onFieldStorageDefinitionUpdate($storage_definition, $original);
}
$this->entityLastInstalledSchemaRepository
->setLastInstalledFieldStorageDefinition($storage_definition);
$this->eventDispatcher
->dispatch(FieldStorageDefinitionEvents::UPDATE, new FieldStorageDefinitionEvent($storage_definition, $original));
$this->entityFieldManager
->clearCachedFieldDefinitions();
}
public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $storage_definition) {
$entity_type_id = $storage_definition
->getTargetEntityTypeId();
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
if ($storage_definition instanceof BaseFieldDefinition && $storage instanceof FieldableEntityStorageInterface && $storage
->countFieldData($storage_definition, TRUE)) {
$deleted_storage_definition = clone $storage_definition;
$deleted_storage_definition
->setDeleted(TRUE);
$this->deletedFieldsRepository
->addFieldDefinition($deleted_storage_definition);
$this->deletedFieldsRepository
->addFieldStorageDefinition($deleted_storage_definition);
}
if ($storage instanceof FieldStorageDefinitionListenerInterface) {
$storage
->onFieldStorageDefinitionDelete($storage_definition);
}
$this->entityLastInstalledSchemaRepository
->deleteLastInstalledFieldStorageDefinition($storage_definition);
$this->eventDispatcher
->dispatch(FieldStorageDefinitionEvents::DELETE, new FieldStorageDefinitionEvent($storage_definition));
$this->entityFieldManager
->clearCachedFieldDefinitions();
}
}