bb17fe6bf0d1016de5ab2c1b9b642fb1f1a0dd70
[yaffs-website] / web / core / lib / Drupal / Core / Field / FieldStorageDefinitionListener.php
1 <?php
2
3 namespace Drupal\Core\Field;
4
5 use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface;
6 use Drupal\Core\Entity\EntityFieldManagerInterface;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9
10 /**
11  * Reacts to field storage definition CRUD on behalf of the Entity system.
12  *
13  * @see \Drupal\Core\Field\FieldStorageDefinitionEvents
14  */
15 class FieldStorageDefinitionListener implements FieldStorageDefinitionListenerInterface {
16
17   /**
18    * The entity type manager.
19    *
20    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
21    */
22   protected $entityTypeManager;
23
24   /**
25    * The event dispatcher.
26    *
27    * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
28    */
29   protected $eventDispatcher;
30
31   /**
32    * The entity definition manager.
33    *
34    * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
35    */
36   protected $entityLastInstalledSchemaRepository;
37
38   /**
39    * The entity field manager.
40    *
41    * @var \Drupal\Core\Entity\EntityFieldManagerInterface
42    */
43   protected $entityFieldManager;
44
45   /**
46    * Constructs a new FieldStorageDefinitionListener.
47    *
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.
56    */
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;
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition) {
68     $entity_type_id = $storage_definition->getTargetEntityTypeId();
69
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);
75     }
76
77     $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::CREATE, new FieldStorageDefinitionEvent($storage_definition));
78
79     $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinition($storage_definition);
80     $this->entityFieldManager->clearCachedFieldDefinitions();
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
87     $entity_type_id = $storage_definition->getTargetEntityTypeId();
88
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);
94     }
95
96     $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::UPDATE, new FieldStorageDefinitionEvent($storage_definition, $original));
97
98     $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinition($storage_definition);
99     $this->entityFieldManager->clearCachedFieldDefinitions();
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $storage_definition) {
106     $entity_type_id = $storage_definition->getTargetEntityTypeId();
107
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);
113     }
114
115     $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::DELETE, new FieldStorageDefinitionEvent($storage_definition));
116
117     $this->entityLastInstalledSchemaRepository->deleteLastInstalledFieldStorageDefinition($storage_definition);
118     $this->entityFieldManager->clearCachedFieldDefinitions();
119   }
120
121 }