3 namespace Drupal\Core\Field;
5 use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface;
6 use Drupal\Core\Entity\EntityFieldManagerInterface;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11 * Reacts to field storage definition CRUD on behalf of the Entity system.
13 * @see \Drupal\Core\Field\FieldStorageDefinitionEvents
15 class FieldStorageDefinitionListener implements FieldStorageDefinitionListenerInterface {
18 * The entity type manager.
20 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
22 protected $entityTypeManager;
25 * The event dispatcher.
27 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
29 protected $eventDispatcher;
32 * The entity definition manager.
34 * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
36 protected $entityLastInstalledSchemaRepository;
39 * The entity field manager.
41 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
43 protected $entityFieldManager;
46 * Constructs a new FieldStorageDefinitionListener.
48 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
49 * The entity type manager.
50 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
51 * The event dispatcher.
52 * @param \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository
53 * The entity last installed schema repository.
54 * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
55 * The entity field manager.
57 public function __construct(EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository, EntityFieldManagerInterface $entity_field_manager) {
58 $this->entityTypeManager = $entity_type_manager;
59 $this->eventDispatcher = $event_dispatcher;
60 $this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository;
61 $this->entityFieldManager = $entity_field_manager;
67 public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition) {
68 $entity_type_id = $storage_definition->getTargetEntityTypeId();
70 // @todo Forward this to all interested handlers, not only storage, once
71 // iterating handlers is possible: https://www.drupal.org/node/2332857.
72 $storage = $this->entityTypeManager->getStorage($entity_type_id);
73 if ($storage instanceof FieldStorageDefinitionListenerInterface) {
74 $storage->onFieldStorageDefinitionCreate($storage_definition);
77 $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::CREATE, new FieldStorageDefinitionEvent($storage_definition));
79 $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinition($storage_definition);
80 $this->entityFieldManager->clearCachedFieldDefinitions();
86 public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
87 $entity_type_id = $storage_definition->getTargetEntityTypeId();
89 // @todo Forward this to all interested handlers, not only storage, once
90 // iterating handlers is possible: https://www.drupal.org/node/2332857.
91 $storage = $this->entityTypeManager->getStorage($entity_type_id);
92 if ($storage instanceof FieldStorageDefinitionListenerInterface) {
93 $storage->onFieldStorageDefinitionUpdate($storage_definition, $original);
96 $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::UPDATE, new FieldStorageDefinitionEvent($storage_definition, $original));
98 $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinition($storage_definition);
99 $this->entityFieldManager->clearCachedFieldDefinitions();
105 public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $storage_definition) {
106 $entity_type_id = $storage_definition->getTargetEntityTypeId();
108 // @todo Forward this to all interested handlers, not only storage, once
109 // iterating handlers is possible: https://www.drupal.org/node/2332857.
110 $storage = $this->entityTypeManager->getStorage($entity_type_id);
111 if ($storage instanceof FieldStorageDefinitionListenerInterface) {
112 $storage->onFieldStorageDefinitionDelete($storage_definition);
115 $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::DELETE, new FieldStorageDefinitionEvent($storage_definition));
117 $this->entityLastInstalledSchemaRepository->deleteLastInstalledFieldStorageDefinition($storage_definition);
118 $this->entityFieldManager->clearCachedFieldDefinitions();