function EntityOperations::entityPreload

Same name in other branches
  1. 9 core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityPreload()
  2. 8.9.x core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityPreload()
  3. 11.x 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 87

Class

EntityOperations
Defines a class for reacting to entity events.

Namespace

Drupal\workspaces

Code

public function entityPreload(array $ids, $entity_type_id) {
    $entities = [];
    $entity_type = $this->entityTypeManager
        ->getDefinition($entity_type_id);
    if (!$this->workspaceInfo
        ->isEntityTypeSupported($entity_type) || !$this->workspaceManager
        ->hasActiveWorkspace()) {
        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)) {
        // Bail out early if there are no tracked entities of this type.
        if (!isset($tracked_entities[$entity_type_id])) {
            return $entities;
        }
        
        /** @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;
}

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