EntityBundleListener.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function onBundleDelete($bundle, $entity_type_id) {
  67. $this->entityTypeBundleInfo->clearCachedBundles();
  68. // Notify the entity storage.
  69. $storage = $this->entityTypeManager->getStorage($entity_type_id);
  70. if ($storage instanceof EntityBundleListenerInterface) {
  71. $storage->onBundleDelete($bundle, $entity_type_id);
  72. }
  73. // Invoke hook_entity_bundle_delete() hook.
  74. $this->moduleHandler->invokeAll('entity_bundle_delete', [$entity_type_id, $bundle]);
  75. $this->entityFieldManager->clearCachedFieldDefinitions();
  76. }
  77. }