3 namespace Drupal\Core\Entity;
5 use Drupal\Core\Extension\ModuleHandlerInterface;
8 * Reacts to entity bundle CRUD on behalf of the Entity system.
10 class EntityBundleListener implements EntityBundleListenerInterface {
13 * The entity type manager.
15 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
17 protected $entityTypeManager;
20 * The entity type bundle info.
22 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
24 protected $entityTypeBundleInfo;
27 * The entity field manager.
29 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
31 protected $entityFieldManager;
36 * @var \Drupal\Core\Extension\ModuleHandlerInterface
38 protected $moduleHandler;
41 * Constructs a new EntityBundleListener.
43 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
44 * The entity type manager.
45 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
46 * The entity type bundle info.
47 * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
48 * The entity field manager.
49 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
52 public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityFieldManagerInterface $entity_field_manager, ModuleHandlerInterface $module_handler) {
53 $this->entityTypeManager = $entity_type_manager;
54 $this->entityTypeBundleInfo = $entity_type_bundle_info;
55 $this->entityFieldManager = $entity_field_manager;
56 $this->moduleHandler = $module_handler;
62 public function onBundleCreate($bundle, $entity_type_id) {
63 $this->entityTypeBundleInfo->clearCachedBundles();
64 // Notify the entity storage.
65 $storage = $this->entityTypeManager->getStorage($entity_type_id);
66 if ($storage instanceof EntityBundleListenerInterface) {
67 $storage->onBundleCreate($bundle, $entity_type_id);
69 // Invoke hook_entity_bundle_create() hook.
70 $this->moduleHandler->invokeAll('entity_bundle_create', [$entity_type_id, $bundle]);
76 public function onBundleDelete($bundle, $entity_type_id) {
77 $this->entityTypeBundleInfo->clearCachedBundles();
78 // Notify the entity storage.
79 $storage = $this->entityTypeManager->getStorage($entity_type_id);
80 if ($storage instanceof EntityBundleListenerInterface) {
81 $storage->onBundleDelete($bundle, $entity_type_id);
83 // Invoke hook_entity_bundle_delete() hook.
84 $this->moduleHandler->invokeAll('entity_bundle_delete', [$entity_type_id, $bundle]);
85 $this->entityFieldManager->clearCachedFieldDefinitions();