diff --git a/core/includes/entity.inc b/core/includes/entity.inc index 02a3ec7..7cb9f2d 100644 --- a/core/includes/entity.inc +++ b/core/includes/entity.inc @@ -41,7 +41,6 @@ function entity_get_info($entity_type = NULL) { */ function entity_info_cache_clear() { drupal_static_reset('entity_get_view_modes'); - drupal_static_reset('entity_get_bundles'); // Clear all languages. Drupal::entityManager()->clearCachedDefinitions(); Drupal::entityManager()->clearCachedFieldDefinitions(); @@ -58,33 +57,12 @@ function entity_info_cache_clear() { * The bundle info for a specific entity type, or all entity types. */ function entity_get_bundles($entity_type = NULL) { - $bundles = &drupal_static(__FUNCTION__); - if (!$bundles) { - $langcode = language(Language::TYPE_INTERFACE)->id; - if ($cache = cache()->get("entity_bundle_info:$langcode")) { - $bundles = $cache->data; - } - else { - $bundles = module_invoke_all('entity_bundle_info'); - // If no bundles are provided, use the entity type name and label. - foreach (entity_get_info() as $type => $entity_info) { - if (!isset($bundles[$type])) { - $bundles[$type][$type]['label'] = $entity_info['label']; - } - } - drupal_alter('entity_bundle_info', $bundles); - cache()->set("entity_bundle_info:$langcode", $bundles, CacheBackendInterface::CACHE_PERMANENT, array('entity_info' => TRUE)); - } + if (isset($entity_type)) { + return Drupal::entityManager()->getBundleInfo($entity_type); } - - if (empty($entity_type)) { - return $bundles; - } - elseif (isset($bundles[$entity_type])) { - return $bundles[$entity_type]; + else { + return Drupal::entityManager()->getAllBundleInfo(); } - - return array(); } /** diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index a662259..a74a918 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -88,6 +88,13 @@ class EntityManager extends PluginManagerBase { protected $fieldDefinitions; /** + * Static cache of bundle information. + * + * @var array + */ + protected $bundleInfo; + + /** * Constructs a new Entity plugin manager. * * @param \Traversable $namespaces @@ -122,6 +129,16 @@ public function __construct(\Traversable $namespaces, ContainerInterface $contai } /** + * {@inheritdoc} + */ + public function clearCachedDefinitions() { + parent::clearCachedDefinitions(); + + $this->bundleInfo = NULL; + } + + + /** * Checks whether a certain entity type has a certain controller. * * @param string $entity_type @@ -458,4 +475,45 @@ public function clearCachedFieldDefinitions() { $this->cache->deleteTags(array('entity_field_info' => TRUE)); } + /** + * Get the bundle info of an entity type. + * + * @param string $entity_type + * The entity type. + * + * @return array + * Returns the bundle information for the specified entity type. + */ + public function getBundleInfo($entity_type) { + $bundle_info = $this->getAllBundleInfo(); + return isset($bundle_info[$entity_type]) ? $bundle_info[$entity_type] : array(); + } + + /** + * Get the bundle info of all entity types. + * + * @return array + * An array of all bundle information. + */ + public function getAllBundleInfo() { + if (!isset($this->bundleInfo)) { + $langcode = $this->languageManager->getLanguage(Language::TYPE_INTERFACE)->id; + if ($cache = $this->cache->get("entity_bundle_info:$langcode")) { + $this->bundleInfo = $cache->data; + } + else { + $this->bundleInfo = $this->moduleHandler->invokeAll('entity_bundle_info'); + // If no bundles are provided, use the entity type name and label. + foreach ($this->getDefinitions() as $type => $entity_info) { + if (!isset($this->bundleInfo[$type])) { + $this->bundleInfo[$type][$type]['label'] = $entity_info['label']; + } + } + $this->moduleHandler->alter('entity_bundle_info', $this->bundleInfo); + $this->cache->set("entity_bundle_info:$langcode", $this->bundleInfo, CacheBackendInterface::CACHE_PERMANENT, array('entity_info' => TRUE)); + } + } + return $this->bundleInfo; + } + }