You are here

public function EntityOperations::entityPreload in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityPreload()

Acts on entity IDs before they are loaded.

See also

hook_entity_preload()

File

core/modules/workspaces/src/EntityOperations.php, line 77

Class

EntityOperations
Defines a class for reacting to entity events.

Namespace

Drupal\workspaces

Code

public function entityPreload(array $ids, $entity_type_id) {
  $entities = [];

  // Only run if the entity type can belong to a workspace and we are in a
  // non-default workspace.
  if (!$this->workspaceManager
    ->shouldAlterOperations($this->entityTypeManager
    ->getDefinition($entity_type_id))) {
    return $entities;
  }

  // Get a list of revision IDs for entities that have a revision set for the
  // current active workspace. If an entity has multiple revisions set for a
  // workspace, only the one with the highest ID is returned.
  if ($tracked_entities = $this->workspaceAssociation
    ->getTrackedEntities($this->workspaceManager
    ->getActiveWorkspace()
    ->id(), $entity_type_id, $ids)) {

    /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
    $storage = $this->entityTypeManager
      ->getStorage($entity_type_id);

    // Swap out every entity which has a revision set for the current active
    // workspace.
    foreach ($storage
      ->loadMultipleRevisions(array_keys($tracked_entities[$entity_type_id])) as $revision) {
      $entities[$revision
        ->id()] = $revision;
    }
  }
  return $entities;
}