function ContentEntityStorageBase::getGroupedEntitiesFromRevisions

Splits revisions into groups which are keyed by entity ID.

Load hooks expect entities to be grouped by entity ID. As we could load multiple revisions for the same entity ID at once we have to build groups of entities where the same entity ID is present only once.

Given 3 revisions, 1 and 2 for entity ID 1 and revision 3 for entity ID 2, it will return the following two groups:

$entity_groups = [
  0 => [
    1 => $revision_1,
    2 => $revision_3,
  ],
  1 => [
    1 => $revision_2,
  ],
];

Parameters

\Drupal\Core\Entity\ContentEntityInterface[] $revisions: List of revisions.

Return value

array<int, array<int, \Drupal\Core\Entity\ContentEntityInterface>> Groups of entities keyed by entity ID.

1 call to ContentEntityStorageBase::getGroupedEntitiesFromRevisions()
ContentEntityStorageBase::loadMultipleRevisions in core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php
Loads multiple entity revisions.

File

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

Class

ContentEntityStorageBase
Base class for content entity storage handlers.

Namespace

Drupal\Core\Entity

Code

protected function getGroupedEntitiesFromRevisions(array $revisions) : array {
  $entity_groups = [];
  $entity_group_mapping = [];
  foreach ($revisions as $revision) {
    $entity_id = $revision->id();
    $entity_group_key = isset($entity_group_mapping[$entity_id]) ? $entity_group_mapping[$entity_id] + 1 : 0;
    $entity_group_mapping[$entity_id] = $entity_group_key;
    $entity_groups[$entity_group_key][$entity_id] = $revision;
  }
  return $entity_groups;
}

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