EntityTypeListener.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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->eventDispatcher->dispatch(EntityTypeEvents::CREATE, new EntityTypeEvent($entity_type));
  64. $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
  65. if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) {
  66. $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinitions($entity_type_id, $this->entityFieldManager->getFieldStorageDefinitions($entity_type_id));
  67. }
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
  73. $entity_type_id = $entity_type->id();
  74. // @todo Forward this to all interested handlers, not only storage, once
  75. // iterating handlers is possible: https://www.drupal.org/node/2332857.
  76. $storage = $this->entityTypeManager->getStorage($entity_type_id);
  77. if ($storage instanceof EntityTypeListenerInterface) {
  78. $storage->onEntityTypeUpdate($entity_type, $original);
  79. }
  80. $this->eventDispatcher->dispatch(EntityTypeEvents::UPDATE, new EntityTypeEvent($entity_type, $original));
  81. $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
  87. $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->onEntityTypeDelete($entity_type);
  93. }
  94. $this->eventDispatcher->dispatch(EntityTypeEvents::DELETE, new EntityTypeEvent($entity_type));
  95. $this->entityLastInstalledSchemaRepository->deleteLastInstalledDefinition($entity_type_id);
  96. }
  97. }