42d00a7fcb1d473e478e5e15ff86217891b0c494
[yaffs-website] / web / core / lib / Drupal / Core / Entity / EntityTypeBundleInfo.php
1 <?php
2
3 namespace Drupal\Core\Entity;
4
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;
11
12 /**
13  * Provides discovery and retrieval of entity type bundles.
14  */
15 class EntityTypeBundleInfo implements EntityTypeBundleInfoInterface {
16
17   use UseCacheBackendTrait;
18
19   /**
20    * Static cache of bundle information.
21    *
22    * @var array
23    */
24   protected $bundleInfo;
25
26   /**
27    * The language manager.
28    *
29    * @var \Drupal\Core\Language\LanguageManagerInterface
30    */
31   protected $languageManager;
32
33   /**
34    * The module handler.
35    *
36    * @var \Drupal\Core\Extension\ModuleHandlerInterface
37    */
38   protected $moduleHandler;
39
40   /**
41    * The typed data manager.
42    *
43    * @var \Drupal\Core\TypedData\TypedDataManagerInterface
44    */
45   protected $typedDataManager;
46
47   /**
48    * The entity type manager.
49    *
50    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
51    */
52   protected $entityTypeManager;
53
54   /**
55    * Constructs a new EntityTypeBundleInfo.
56    *
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
62    *   The module handler.
63    * @param \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data_manager
64    *   The typed data manager.
65    * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
66    *   The cache backend.
67    */
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;
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function getBundleInfo($entity_type) {
80     $bundle_info = $this->getAllBundleInfo();
81     return isset($bundle_info[$entity_type]) ? $bundle_info[$entity_type] : [];
82   }
83
84   /**
85    * {@inheritdoc}
86    */
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;
92       }
93       else {
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();
101             }
102           }
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();
108           }
109         }
110         $this->moduleHandler->alter('entity_bundle_info', $this->bundleInfo);
111         $this->cacheSet("entity_bundle_info:$langcode", $this->bundleInfo, Cache::PERMANENT, ['entity_types', 'entity_bundles']);
112       }
113     }
114
115     return $this->bundleInfo;
116   }
117
118   /**
119    * {@inheritdoc}
120    */
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();
126   }
127
128 }