EntityBundleListener.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Core\Extension\ModuleHandlerInterface;
  4. /**
  5. * Reacts to entity bundle CRUD on behalf of the Entity system.
  6. */
  7. class EntityBundleListener implements EntityBundleListenerInterface {
  8. /**
  9. * The entity type manager.
  10. *
  11. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  12. */
  13. protected $entityTypeManager;
  14. /**
  15. * The entity type bundle info.
  16. *
  17. * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
  18. */
  19. protected $entityTypeBundleInfo;
  20. /**
  21. * The entity field manager.
  22. *
  23. * @var \Drupal\Core\Entity\EntityFieldManagerInterface
  24. */
  25. protected $entityFieldManager;
  26. /**
  27. * The module handler.
  28. *
  29. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  30. */
  31. protected $moduleHandler;
  32. /**
  33. * Constructs a new EntityBundleListener.
  34. *
  35. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  36. * The entity type manager.
  37. * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
  38. * The entity type bundle info.
  39. * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
  40. * The entity field manager.
  41. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  42. * The module handler.
  43. */
  44. public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityFieldManagerInterface $entity_field_manager, ModuleHandlerInterface $module_handler) {
  45. $this->entityTypeManager = $entity_type_manager;
  46. $this->entityTypeBundleInfo = $entity_type_bundle_info;
  47. $this->entityFieldManager = $entity_field_manager;
  48. $this->moduleHandler = $module_handler;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function onBundleCreate($bundle, $entity_type_id) {
  54. $this->entityTypeBundleInfo->clearCachedBundles();
  55. // Notify the entity storage.
  56. $storage = $this->entityTypeManager->getStorage($entity_type_id);
  57. if ($storage instanceof EntityBundleListenerInterface) {
  58. $storage->onBundleCreate($bundle, $entity_type_id);
  59. }
  60. // Invoke hook_entity_bundle_create() hook.
  61. $this->moduleHandler->invokeAll('entity_bundle_create', [$entity_type_id, $bundle]);
  62. $this->entityFieldManager->clearCachedFieldDefinitions();
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function onBundleDelete($bundle, $entity_type_id) {
  68. $this->entityTypeBundleInfo->clearCachedBundles();
  69. // Notify the entity storage.
  70. $storage = $this->entityTypeManager->getStorage($entity_type_id);
  71. if ($storage instanceof EntityBundleListenerInterface) {
  72. $storage->onBundleDelete($bundle, $entity_type_id);
  73. }
  74. // Invoke hook_entity_bundle_delete() hook.
  75. $this->moduleHandler->invokeAll('entity_bundle_delete', [$entity_type_id, $bundle]);
  76. $this->entityFieldManager->clearCachedFieldDefinitions();
  77. }
  78. }