function ConfigEntityStorage::doLoadMultiple

Same name in other branches
  1. 9 core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php \Drupal\Core\Config\Entity\ConfigEntityStorage::doLoadMultiple()
  2. 10 core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php \Drupal\Core\Config\Entity\ConfigEntityStorage::doLoadMultiple()
  3. 11.x core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php \Drupal\Core\Config\Entity\ConfigEntityStorage::doLoadMultiple()

Overrides EntityStorageBase::doLoadMultiple

File

core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php, line 169

Class

ConfigEntityStorage
Defines the storage class for configuration entities.

Namespace

Drupal\Core\Config\Entity

Code

protected function doLoadMultiple(array $ids = NULL) {
    $prefix = $this->getPrefix();
    // Get the names of the configuration entities we are going to load.
    if ($ids === NULL) {
        $names = $this->configFactory
            ->listAll($prefix);
    }
    else {
        $names = [];
        foreach ($ids as $id) {
            // Add the prefix to the ID to serve as the configuration object name.
            $names[] = $prefix . $id;
        }
    }
    // Load all of the configuration entities.
    
    /** @var \Drupal\Core\Config\Config[] $configs */
    $configs = [];
    $records = [];
    foreach ($this->configFactory
        ->loadMultiple($names) as $config) {
        $id = $config->get($this->idKey);
        $records[$id] = $this->overrideFree ? $config->getOriginal(NULL, FALSE) : $config->get();
        $configs[$id] = $config;
    }
    $entities = $this->mapFromStorageRecords($records, $configs);
    // Config entities wrap config objects, and therefore they need to inherit
    // the cacheability metadata of config objects (to ensure e.g. additional
    // cacheability metadata added by config overrides is not lost).
    foreach ($entities as $id => $entity) {
        // But rather than simply inheriting all cacheability metadata of config
        // objects, we need to make sure the self-referring cache tag that is
        // present on Config objects is not added to the Config entity. It must be
        // removed for 3 reasons:
        // 1. When renaming/duplicating a Config entity, the cache tag of the
        //    original config object would remain present, which would be wrong.
        // 2. Some Config entities choose to not use the cache tag that the under-
        //    lying Config object provides by default (For performance and
        //    cacheability reasons it may not make sense to have a unique cache
        //    tag for every Config entity. The DateFormat Config entity specifies
        //    the 'rendered' cache tag for example, because A) date formats are
        //    changed extremely rarely, so invalidating all render cache items is
        //    fine, B) it means fewer cache tags per page.).
        // 3. Fewer cache tags is better for performance.
        $self_referring_cache_tag = [
            'config:' . $configs[$id]->getName(),
        ];
        $config_cacheability = CacheableMetadata::createFromObject($configs[$id]);
        $config_cacheability->setCacheTags(array_diff($config_cacheability->getCacheTags(), $self_referring_cache_tag));
        $entity->addCacheableDependency($config_cacheability);
    }
    return $entities;
}

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