You are here

protected function ConfigEntityStorage::doLoadMultiple in Drupal 9

Same name and namespace in other branches
  1. 8 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()

Performs storage-specific loading of entities.

Override this method to add custom functionality directly after loading. This is always called, while self::postLoad() is only called when there are actual results.

Parameters

array|null $ids: (optional) An array of entity IDs, or NULL to load all entities.

Return value

\Drupal\Core\Entity\EntityInterface[] Associative array of entities, keyed on the entity ID.

Overrides EntityStorageBase::doLoadMultiple

File

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

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);

  // 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;
}