class EntityTypeBundleInfo

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Entity/EntityTypeBundleInfo.php \Drupal\Core\Entity\EntityTypeBundleInfo
  2. 10 core/lib/Drupal/Core/Entity/EntityTypeBundleInfo.php \Drupal\Core\Entity\EntityTypeBundleInfo
  3. 11.x core/lib/Drupal/Core/Entity/EntityTypeBundleInfo.php \Drupal\Core\Entity\EntityTypeBundleInfo

Provides discovery and retrieval of entity type bundles.

Hierarchy

Expanded class hierarchy of EntityTypeBundleInfo

1 file declares its use of EntityTypeBundleInfo
EntityTypeBundleInfoTest.php in core/tests/Drupal/Tests/Core/Entity/EntityTypeBundleInfoTest.php
2 string references to 'EntityTypeBundleInfo'
core.services.yml in core/core.services.yml
core/core.services.yml
EntityUrlTest::getEntity in core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php
Returns a mock entity for testing.
1 service uses EntityTypeBundleInfo
entity_type.bundle.info in core/core.services.yml
Drupal\Core\Entity\EntityTypeBundleInfo

File

core/lib/Drupal/Core/Entity/EntityTypeBundleInfo.php, line 15

Namespace

Drupal\Core\Entity
View source
class EntityTypeBundleInfo implements EntityTypeBundleInfoInterface {
    use UseCacheBackendTrait;
    
    /**
     * Static cache of bundle information.
     *
     * @var array
     */
    protected $bundleInfo;
    
    /**
     * The language manager.
     *
     * @var \Drupal\Core\Language\LanguageManagerInterface
     */
    protected $languageManager;
    
    /**
     * The module handler.
     *
     * @var \Drupal\Core\Extension\ModuleHandlerInterface
     */
    protected $moduleHandler;
    
    /**
     * The typed data manager.
     *
     * @var \Drupal\Core\TypedData\TypedDataManagerInterface
     */
    protected $typedDataManager;
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * Constructs a new EntityTypeBundleInfo.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager.
     * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
     *   The language manager.
     * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
     *   The module handler.
     * @param \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data_manager
     *   The typed data manager.
     * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
     *   The cache backend.
     */
    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;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getBundleInfo($entity_type_id) {
        $bundle_info = $this->getAllBundleInfo();
        return $bundle_info[$entity_type_id] ?? [];
    }
    
    /**
     * {@inheritdoc}
     */
    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) {
                    // First look for entity types that act as bundles for others, load them
                    // and add them as bundles.
                    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;
    }
    
    /**
     * {@inheritdoc}
     */
    public function clearCachedBundles() {
        $this->bundleInfo = [];
        Cache::invalidateTags([
            'entity_bundles',
        ]);
        // Entity bundles are exposed as data types, clear that cache too.
        $this->typedDataManager
            ->clearCachedDefinitions();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
EntityTypeBundleInfo::$bundleInfo protected property Static cache of bundle information.
EntityTypeBundleInfo::$entityTypeManager protected property The entity type manager.
EntityTypeBundleInfo::$languageManager protected property The language manager.
EntityTypeBundleInfo::$moduleHandler protected property The module handler.
EntityTypeBundleInfo::$typedDataManager protected property The typed data manager.
EntityTypeBundleInfo::clearCachedBundles public function Clears static and persistent bundles. Overrides EntityTypeBundleInfoInterface::clearCachedBundles
EntityTypeBundleInfo::getAllBundleInfo public function Get the bundle info of all entity types. Overrides EntityTypeBundleInfoInterface::getAllBundleInfo
EntityTypeBundleInfo::getBundleInfo public function Gets the bundle info of an entity type. Overrides EntityTypeBundleInfoInterface::getBundleInfo
EntityTypeBundleInfo::__construct public function Constructs a new EntityTypeBundleInfo.
UseCacheBackendTrait::$cacheBackend protected property Cache backend instance.
UseCacheBackendTrait::$useCaches protected property Flag whether caches should be used or skipped.
UseCacheBackendTrait::cacheGet protected function Fetches from the cache backend, respecting the use caches flag.
UseCacheBackendTrait::cacheSet protected function Stores data in the persistent cache, respecting the use caches flag.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.