3 namespace Drupal\Core\Entity;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\Core\Cache\UseCacheBackendTrait;
8 use Drupal\Core\Extension\ModuleHandlerInterface;
9 use Drupal\Core\Language\LanguageManagerInterface;
10 use Drupal\Core\TypedData\TypedDataManagerInterface;
13 * Provides discovery and retrieval of entity type bundles.
15 class EntityTypeBundleInfo implements EntityTypeBundleInfoInterface {
17 use UseCacheBackendTrait;
20 * Static cache of bundle information.
24 protected $bundleInfo;
27 * The language manager.
29 * @var \Drupal\Core\Language\LanguageManagerInterface
31 protected $languageManager;
36 * @var \Drupal\Core\Extension\ModuleHandlerInterface
38 protected $moduleHandler;
41 * The typed data manager.
43 * @var \Drupal\Core\TypedData\TypedDataManagerInterface
45 protected $typedDataManager;
48 * The entity type manager.
50 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
52 protected $entityTypeManager;
55 * Constructs a new EntityTypeBundleInfo.
57 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
58 * The entity type manager.
59 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
60 * The language manager.
61 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
63 * @param \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data_manager
64 * The typed data manager.
65 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
68 public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, ModuleHandlerInterface $module_handler, TypedDataManagerInterface $typed_data_manager, CacheBackendInterface $cache_backend) {
69 $this->entityTypeManager = $entity_type_manager;
70 $this->languageManager = $language_manager;
71 $this->moduleHandler = $module_handler;
72 $this->typedDataManager = $typed_data_manager;
73 $this->cacheBackend = $cache_backend;
79 public function getBundleInfo($entity_type) {
80 $bundle_info = $this->getAllBundleInfo();
81 return isset($bundle_info[$entity_type]) ? $bundle_info[$entity_type] : [];
87 public function getAllBundleInfo() {
88 if (empty($this->bundleInfo)) {
89 $langcode = $this->languageManager->getCurrentLanguage()->getId();
90 if ($cache = $this->cacheGet("entity_bundle_info:$langcode")) {
91 $this->bundleInfo = $cache->data;
94 $this->bundleInfo = $this->moduleHandler->invokeAll('entity_bundle_info');
95 foreach ($this->entityTypeManager->getDefinitions() as $type => $entity_type) {
96 // First look for entity types that act as bundles for others, load them
97 // and add them as bundles.
98 if ($bundle_entity_type = $entity_type->getBundleEntityType()) {
99 foreach ($this->entityTypeManager->getStorage($bundle_entity_type)->loadMultiple() as $entity) {
100 $this->bundleInfo[$type][$entity->id()]['label'] = $entity->label();
103 // If entity type bundles are not supported and
104 // hook_entity_bundle_info() has not already set up bundle
105 // information, use the entity type name and label.
106 elseif (!isset($this->bundleInfo[$type])) {
107 $this->bundleInfo[$type][$type]['label'] = $entity_type->getLabel();
110 $this->moduleHandler->alter('entity_bundle_info', $this->bundleInfo);
111 $this->cacheSet("entity_bundle_info:$langcode", $this->bundleInfo, Cache::PERMANENT, ['entity_types', 'entity_bundles']);
115 return $this->bundleInfo;
121 public function clearCachedBundles() {
122 $this->bundleInfo = [];
123 Cache::invalidateTags(['entity_bundles']);
124 // Entity bundles are exposed as data types, clear that cache too.
125 $this->typedDataManager->clearCachedDefinitions();