Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / lib / Drupal / Core / Entity / EntityTypeListener.php
1 <?php
2
3 namespace Drupal\Core\Entity;
4
5 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
6
7 /**
8  * Reacts to entity type CRUD on behalf of the Entity system.
9  *
10  * @see \Drupal\Core\Entity\EntityTypeEvents
11  */
12 class EntityTypeListener implements EntityTypeListenerInterface {
13
14   /**
15    * The entity type manager.
16    *
17    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
18    */
19   protected $entityTypeManager;
20
21   /**
22    * The entity field manager.
23    *
24    * @var \Drupal\Core\Entity\EntityFieldManagerInterface
25    */
26   protected $entityFieldManager;
27
28   /**
29    * The event dispatcher.
30    *
31    * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
32    */
33   protected $eventDispatcher;
34
35   /**
36    * The entity last installed schema repository.
37    *
38    * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
39    */
40   protected $entityLastInstalledSchemaRepository;
41
42   /**
43    * Constructs a new EntityTypeListener.
44    *
45    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
46    *   The entity type manager.
47    * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
48    *   The entity field manager.
49    * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
50    *   The event dispatcher.
51    * @param \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository
52    *   The entity last installed schema repository.
53    */
54   public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, EventDispatcherInterface $event_dispatcher, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository) {
55     $this->entityTypeManager = $entity_type_manager;
56     $this->entityFieldManager = $entity_field_manager;
57     $this->eventDispatcher = $event_dispatcher;
58     $this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository;
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
65     $entity_type_id = $entity_type->id();
66
67     // @todo Forward this to all interested handlers, not only storage, once
68     //   iterating handlers is possible: https://www.drupal.org/node/2332857.
69     $storage = $this->entityTypeManager->getStorage($entity_type_id);
70     if ($storage instanceof EntityTypeListenerInterface) {
71       $storage->onEntityTypeCreate($entity_type);
72     }
73
74     $this->eventDispatcher->dispatch(EntityTypeEvents::CREATE, new EntityTypeEvent($entity_type));
75
76     $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
77     if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) {
78       $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinitions($entity_type_id, $this->entityFieldManager->getFieldStorageDefinitions($entity_type_id));
79     }
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
86     $entity_type_id = $entity_type->id();
87
88     // @todo Forward this to all interested handlers, not only storage, once
89     //   iterating handlers is possible: https://www.drupal.org/node/2332857.
90     $storage = $this->entityTypeManager->getStorage($entity_type_id);
91     if ($storage instanceof EntityTypeListenerInterface) {
92       $storage->onEntityTypeUpdate($entity_type, $original);
93     }
94
95     $this->eventDispatcher->dispatch(EntityTypeEvents::UPDATE, new EntityTypeEvent($entity_type, $original));
96
97     $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
104     $entity_type_id = $entity_type->id();
105
106     // @todo Forward this to all interested handlers, not only storage, once
107     //   iterating handlers is possible: https://www.drupal.org/node/2332857.
108     $storage = $this->entityTypeManager->getStorage($entity_type_id);
109     if ($storage instanceof EntityTypeListenerInterface) {
110       $storage->onEntityTypeDelete($entity_type);
111     }
112
113     $this->eventDispatcher->dispatch(EntityTypeEvents::DELETE, new EntityTypeEvent($entity_type));
114
115     $this->entityLastInstalledSchemaRepository->deleteLastInstalledDefinition($entity_type_id);
116   }
117
118 }