EntityTypeBundleInfo.php in Drupal 8
File
core/lib/Drupal/Core/Entity/EntityTypeBundleInfo.php
View source
<?php
namespace Drupal\Core\Entity;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\UseCacheBackendTrait;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\TypedData\TypedDataManagerInterface;
class EntityTypeBundleInfo implements EntityTypeBundleInfoInterface {
use UseCacheBackendTrait;
protected $bundleInfo;
protected $languageManager;
protected $moduleHandler;
protected $typedDataManager;
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, ModuleHandlerInterface $module_handler, TypedDataManagerInterface $typed_data_manager, CacheBackendInterface $cache_backend) {
$this->entityTypeManager = $entity_type_manager;
$this->languageManager = $language_manager;
$this->moduleHandler = $module_handler;
$this->typedDataManager = $typed_data_manager;
$this->cacheBackend = $cache_backend;
}
public function getBundleInfo($entity_type_id) {
$bundle_info = $this
->getAllBundleInfo();
return isset($bundle_info[$entity_type_id]) ? $bundle_info[$entity_type_id] : [];
}
public function getAllBundleInfo() {
if (empty($this->bundleInfo)) {
$langcode = $this->languageManager
->getCurrentLanguage()
->getId();
if ($cache = $this
->cacheGet("entity_bundle_info:{$langcode}")) {
$this->bundleInfo = $cache->data;
}
else {
$this->bundleInfo = $this->moduleHandler
->invokeAll('entity_bundle_info');
foreach ($this->entityTypeManager
->getDefinitions() as $type => $entity_type) {
if ($bundle_entity_type = $entity_type
->getBundleEntityType()) {
foreach ($this->entityTypeManager
->getStorage($bundle_entity_type)
->loadMultiple() as $entity) {
$this->bundleInfo[$type][$entity
->id()]['label'] = $entity
->label();
}
}
elseif (!isset($this->bundleInfo[$type])) {
$this->bundleInfo[$type][$type]['label'] = $entity_type
->getLabel();
}
}
$this->moduleHandler
->alter('entity_bundle_info', $this->bundleInfo);
$this
->cacheSet("entity_bundle_info:{$langcode}", $this->bundleInfo, Cache::PERMANENT, [
'entity_types',
'entity_bundles',
]);
}
}
return $this->bundleInfo;
}
public function clearCachedBundles() {
$this->bundleInfo = [];
Cache::invalidateTags([
'entity_bundles',
]);
$this->typedDataManager
->clearCachedDefinitions();
}
}