Commit ea8a4bdb authored by alexpott's avatar alexpott

Issue #2030151 by damiankloip, dawehner: Convert entity_get_bundles() to a...

Issue #2030151 by damiankloip, dawehner: Convert entity_get_bundles() to a method on the entity manager.
parent 11aead58
......@@ -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();
}
/**
......
......@@ -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.
*
......@@ -165,6 +172,16 @@ public function addNamespaces(\Traversable $namespaces) {
$this->doDiscovery($iterator);
}
/**
* {@inheritdoc}
*/
public function clearCachedDefinitions() {
parent::clearCachedDefinitions();
$this->bundleInfo = NULL;
}
/**
* Checks whether a certain entity type has a certain controller.
*
......@@ -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;
}
}
......@@ -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');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment