diff --git a/core/includes/entity.inc b/core/includes/entity.inc index d49f5cd..2590ce9 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(); @@ -56,35 +55,17 @@ function entity_info_cache_clear() { * * @return array * The bundle info for a specific entity type, or all entity types. + * + * @see \Drupal\Core\Entity\EntityManager::getBundleInfo() + * @see \Drupal\Core\Entity\EntityManager::getAllBundleInfo() */ 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 = Drupal::moduleHandler()->invokeAll('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 3060909..b8ff7bc 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -104,6 +104,13 @@ class EntityManager extends PluginManagerBase { */ protected $translationManager; + /* + * Static cache of bundle information. + * + * @var array + */ + protected $bundleInfo; + /** * Constructs a new Entity plugin manager. * @@ -166,6 +173,16 @@ public function addNamespaces(\Traversable $namespaces) { } /** + * {@inheritdoc} + */ + public function clearCachedDefinitions() { + parent::clearCachedDefinitions(); + + $this->bundleInfo = NULL; + } + + + /** * Checks whether a certain entity type has a certain controller. * * @param string $entity_type @@ -511,4 +528,46 @@ 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; + } + } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAdminTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAdminTest.php index 932f55f..bdf0b3e 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAdminTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAdminTest.php @@ -88,6 +88,8 @@ public function testFieldAdminHandler() { $this->assertFieldByName('instance[settings][handler_settings][target_bundles][' . $bundle_name . ']'); } + reset($bundles); + // Test the sort settings. // Option 0: no sort. $this->assertFieldByName('instance[settings][handler_settings][sort][field]', '_none');