EntityTypeListener.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  4. /**
  5. * Reacts to entity type CRUD on behalf of the Entity system.
  6. *
  7. * @see \Drupal\Core\Entity\EntityTypeEvents
  8. */
  9. class EntityTypeListener implements EntityTypeListenerInterface {
  10. /**
  11. * The entity type manager.
  12. *
  13. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  14. */
  15. protected $entityTypeManager;
  16. /**
  17. * The entity field manager.
  18. *
  19. * @var \Drupal\Core\Entity\EntityFieldManagerInterface
  20. */
  21. protected $entityFieldManager;
  22. /**
  23. * The event dispatcher.
  24. *
  25. * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
  26. */
  27. protected $eventDispatcher;
  28. /**
  29. * The entity last installed schema repository.
  30. *
  31. * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
  32. */
  33. protected $entityLastInstalledSchemaRepository;
  34. /**
  35. * Constructs a new EntityTypeListener.
  36. *
  37. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  38. * The entity type manager.
  39. * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
  40. * The entity field manager.
  41. * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
  42. * The event dispatcher.
  43. * @param \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository
  44. * The entity last installed schema repository.
  45. */
  46. public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, EventDispatcherInterface $event_dispatcher, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository) {
  47. $this->entityTypeManager = $entity_type_manager;
  48. $this->entityFieldManager = $entity_field_manager;
  49. $this->eventDispatcher = $event_dispatcher;
  50. $this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
  56. $entity_type_id = $entity_type->id();
  57. // @todo Forward this to all interested handlers, not only storage, once
  58. // iterating handlers is possible: https://www.drupal.org/node/2332857.
  59. $storage = $this->entityTypeManager->getStorage($entity_type_id);
  60. if ($storage instanceof EntityTypeListenerInterface) {
  61. $storage->onEntityTypeCreate($entity_type);
  62. }
  63. $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
  64. if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) {
  65. $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinitions($entity_type_id, $this->entityFieldManager->getFieldStorageDefinitions($entity_type_id));
  66. }
  67. $this->eventDispatcher->dispatch(EntityTypeEvents::CREATE, new EntityTypeEvent($entity_type));
  68. $this->clearCachedDefinitions();
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function onFieldableEntityTypeCreate(EntityTypeInterface $entity_type, array $field_storage_definitions) {
  74. $entity_type_id = $entity_type->id();
  75. // @todo Forward this to all interested handlers, not only storage, once
  76. // iterating handlers is possible: https://www.drupal.org/node/2332857.
  77. $storage = $this->entityTypeManager->createHandlerInstance($entity_type->getStorageClass(), $entity_type);
  78. if ($storage instanceof EntityTypeListenerInterface) {
  79. $storage->onFieldableEntityTypeCreate($entity_type, $field_storage_definitions);
  80. }
  81. $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
  82. if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) {
  83. $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinitions($entity_type_id, $field_storage_definitions);
  84. }
  85. $this->eventDispatcher->dispatch(EntityTypeEvents::CREATE, new EntityTypeEvent($entity_type));
  86. $this->clearCachedDefinitions();
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
  92. // An entity type can be updated even when its live (in-code) definition has
  93. // been removed from the codebase, so we need to instantiate a custom
  94. // storage handler that uses the passed-in entity type definition.
  95. $storage = $this->entityTypeManager->createHandlerInstance($entity_type->getStorageClass(), $entity_type);
  96. // @todo Forward this to all interested handlers, not only storage, once
  97. // iterating handlers is possible: https://www.drupal.org/node/2332857.
  98. if ($storage instanceof EntityTypeListenerInterface) {
  99. $storage->onEntityTypeUpdate($entity_type, $original);
  100. }
  101. $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
  102. $this->eventDispatcher->dispatch(EntityTypeEvents::UPDATE, new EntityTypeEvent($entity_type, $original));
  103. $this->clearCachedDefinitions();
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
  109. $entity_type_id = $entity_type->id();
  110. // An entity type can be deleted even when its live (in-code) definition has
  111. // been removed from the codebase, so we need to instantiate a custom
  112. // storage handler that uses the passed-in entity type definition.
  113. $storage = $this->entityTypeManager->createHandlerInstance($entity_type->getStorageClass(), $entity_type);
  114. // @todo Forward this to all interested handlers, not only storage, once
  115. // iterating handlers is possible: https://www.drupal.org/node/2332857.
  116. if ($storage instanceof EntityTypeListenerInterface) {
  117. $storage->onEntityTypeDelete($entity_type);
  118. }
  119. $this->entityLastInstalledSchemaRepository->deleteLastInstalledDefinition($entity_type_id);
  120. $this->eventDispatcher->dispatch(EntityTypeEvents::DELETE, new EntityTypeEvent($entity_type));
  121. $this->clearCachedDefinitions();
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function onFieldableEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original, array $field_storage_definitions, array $original_field_storage_definitions, array &$sandbox = NULL) {
  127. $entity_type_id = $entity_type->id();
  128. // @todo Forward this to all interested handlers, not only storage, once
  129. // iterating handlers is possible: https://www.drupal.org/node/2332857.
  130. $storage = $this->entityTypeManager->createHandlerInstance($entity_type->getStorageClass(), $entity_type);
  131. if ($storage instanceof EntityTypeListenerInterface) {
  132. $storage->onFieldableEntityTypeUpdate($entity_type, $original, $field_storage_definitions, $original_field_storage_definitions, $sandbox);
  133. }
  134. if ($sandbox === NULL || (isset($sandbox['#finished']) && $sandbox['#finished'] == 1)) {
  135. $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
  136. if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) {
  137. $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinitions($entity_type_id, $field_storage_definitions);
  138. }
  139. $this->eventDispatcher->dispatch(EntityTypeEvents::UPDATE, new EntityTypeEvent($entity_type, $original));
  140. $this->clearCachedDefinitions();
  141. }
  142. }
  143. /**
  144. * Clears necessary caches to apply entity/field definition updates.
  145. */
  146. protected function clearCachedDefinitions() {
  147. $this->entityTypeManager->clearCachedDefinitions();
  148. $this->entityFieldManager->clearCachedFieldDefinitions();
  149. }
  150. }