3 namespace Drupal\Core\Entity;
5 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
8 * Reacts to entity type CRUD on behalf of the Entity system.
10 * @see \Drupal\Core\Entity\EntityTypeEvents
12 class EntityTypeListener implements EntityTypeListenerInterface {
15 * The entity type manager.
17 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
19 protected $entityTypeManager;
22 * The entity field manager.
24 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
26 protected $entityFieldManager;
29 * The event dispatcher.
31 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
33 protected $eventDispatcher;
36 * The entity last installed schema repository.
38 * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
40 protected $entityLastInstalledSchemaRepository;
43 * Constructs a new EntityTypeListener.
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.
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;
64 public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
65 $entity_type_id = $entity_type->id();
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);
74 $this->eventDispatcher->dispatch(EntityTypeEvents::CREATE, new EntityTypeEvent($entity_type));
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));
85 public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
86 $entity_type_id = $entity_type->id();
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);
95 $this->eventDispatcher->dispatch(EntityTypeEvents::UPDATE, new EntityTypeEvent($entity_type, $original));
97 $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
103 public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
104 $entity_type_id = $entity_type->id();
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);
113 $this->eventDispatcher->dispatch(EntityTypeEvents::DELETE, new EntityTypeEvent($entity_type));
115 $this->entityLastInstalledSchemaRepository->deleteLastInstalledDefinition($entity_type_id);