function ContentEntityStorageBase::loadMultipleRevisions

Loads multiple entity revisions.

Parameters

int[]|string[] $revision_ids: An array of revision IDs to load.

Return value

\Drupal\Core\Entity\RevisionableInterface[] An array of entity revisions keyed by their revision ID, or an empty array if none found.

Overrides RevisionableStorageInterface::loadMultipleRevisions

1 call to ContentEntityStorageBase::loadMultipleRevisions()
ContentEntityStorageBase::loadRevision in core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php
Loads a specific entity revision.
1 method overrides ContentEntityStorageBase::loadMultipleRevisions()
ContentEntityNullStorage::loadMultipleRevisions in core/lib/Drupal/Core/Entity/ContentEntityNullStorage.php
Loads multiple entity revisions.

File

core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php, line 658

Class

ContentEntityStorageBase
Base class for content entity storage handlers.

Namespace

Drupal\Core\Entity

Code

public function loadMultipleRevisions(array $revision_ids) {
  $revisions = [];
  // Create a new variable which is a prepared version of the
  // $revision_ids array for later comparison with the revision cache. The
  // $revision_ids array is reduced as items are loaded from cache, allowing
  // storage queries to be avoided.
  $flipped_revision_ids = array_flip($revision_ids);
  // Try to load entities from the static cache.
  if (!$this->ignoreStaticRevisionCache && $revision_ids) {
    $revisions += $this->getFromStaticRevisionCache($revision_ids);
    // If any revisions were loaded, remove them from the ids still to load.
    if ($flipped_revision_ids && $revisions) {
      $revision_ids = array_keys(array_diff_key($flipped_revision_ids, $revisions));
    }
  }
  // Attempt to load entities from the persistent cache. This will remove IDs
  // that were loaded from $revision_ids.
  $persistent_cache_revisions = $this->getFromPersistentRevisionCache($revision_ids);
  // Invoke post load on those revisions.
  foreach ($this->getGroupedEntitiesFromRevisions($persistent_cache_revisions) as $entities) {
    $this->postLoad($entities);
  }
  $revisions += $persistent_cache_revisions;
  // Load any remaining revisions from the storage. This is the case if there
  // are any revision IDs left to load.
  $queried_revisions = [];
  if ($revision_ids) {
    $queried_revisions = $this->doLoadMultipleRevisionsFieldItems($revision_ids);
    // Pass all revisions loaded from the database through $this->postLoad(),
    // which attaches fields (if supported by the entity type) and calls the
    // entity type specific load callback, for example hook_node_load().
    if ($queried_revisions) {
      $entity_groups = $this->getGroupedEntitiesFromRevisions($queried_revisions);
      // Invoke the entity hooks for each group, store the entities in the
      // persistent cache between the storage load hooks and the regular post
      // load processing.
      foreach ($entity_groups as $entities) {
        $this->invokeStorageLoadHook($entities);
      }
      $this->setPersistentRevisionCache($queried_revisions);
      foreach ($entity_groups as $entities) {
        $this->postLoad($entities);
      }
      $revisions += $queried_revisions;
    }
  }
  if (!$this->ignoreStaticRevisionCache && $this->entityType
    ->isStaticallyCacheable()) {
    // Add revisions to the static cache, but only with the revision ID.
    // This avoids issues with entity preloading as that might not
    // result in actually being the default revision.
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    foreach ($queried_revisions + $persistent_cache_revisions as $entity) {
      $id = $entity->id();
      // @see \Drupal\Core\Entity\ContentEntityStorageBase::setPersistentCache()
      $cache_tags_revision = [
        $this->memoryCacheTag,
        "{$this->entityTypeId}:{$id}:revisions",
      ];
      $this->memoryCache
        ->set($this->buildRevisionCacheId($entity->getRevisionId()), $entity, MemoryCacheInterface::CACHE_PERMANENT, $cache_tags_revision);
    }
  }
  // Ensure that the returned array is ordered the same as the original
  // $revision_ids array if this was passed in and remove any invalid revision
  // IDs.
  if ($flipped_revision_ids) {
    // Remove any invalid revision IDs from the array.
    $flipped_revision_ids = array_intersect_key($flipped_revision_ids, $revisions);
    foreach ($revisions as $revision_id => $revision) {
      $flipped_revision_ids[$revision_id] = $revision;
    }
    $revisions = $flipped_revision_ids;
  }
  return $revisions;
}

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