function WorkspaceAssociation::getAssociatedRevisions

Same name in other branches
  1. 9 core/modules/workspaces/src/WorkspaceAssociation.php \Drupal\workspaces\WorkspaceAssociation::getAssociatedRevisions()
  2. 8.9.x core/modules/workspaces/src/WorkspaceAssociation.php \Drupal\workspaces\WorkspaceAssociation::getAssociatedRevisions()
  3. 10 core/modules/workspaces/src/WorkspaceAssociation.php \Drupal\workspaces\WorkspaceAssociation::getAssociatedRevisions()

File

core/modules/workspaces/src/WorkspaceAssociation.php, line 180

Class

WorkspaceAssociation
Provides a class for CRUD operations on workspace associations.

Namespace

Drupal\workspaces

Code

public function getAssociatedRevisions($workspace_id, $entity_type_id, $entity_ids = NULL) {
    if (isset($this->associatedRevisions[$workspace_id][$entity_type_id])) {
        if ($entity_ids) {
            return array_intersect($this->associatedRevisions[$workspace_id][$entity_type_id], $entity_ids);
        }
        else {
            return $this->associatedRevisions[$workspace_id][$entity_type_id];
        }
    }
    
    /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
    $storage = $this->entityTypeManager
        ->getStorage($entity_type_id);
    // If the entity type is not using core's default entity storage, we can't
    // assume the table mapping layout so we have to return only the latest
    // tracked revisions.
    if (!$storage instanceof SqlContentEntityStorage) {
        return $this->getTrackedEntities($workspace_id, $entity_type_id, $entity_ids)[$entity_type_id];
    }
    $entity_type = $storage->getEntityType();
    $table_mapping = $storage->getTableMapping();
    $workspace_field = $table_mapping->getColumnNames($entity_type->get('revision_metadata_keys')['workspace'])['target_id'];
    $id_field = $table_mapping->getColumnNames($entity_type->getKey('id'))['value'];
    $revision_id_field = $table_mapping->getColumnNames($entity_type->getKey('revision'))['value'];
    $workspace_tree = $this->workspaceRepository
        ->loadTree();
    if (isset($workspace_tree[$workspace_id])) {
        $workspace_candidates = array_merge([
            $workspace_id,
        ], $workspace_tree[$workspace_id]['ancestors']);
    }
    else {
        $workspace_candidates = [
            $workspace_id,
        ];
    }
    $query = $this->database
        ->select($entity_type->getRevisionTable(), 'revision');
    $query->leftJoin($entity_type->getBaseTable(), 'base', "[revision].[{$id_field}] = [base].[{$id_field}]");
    $query->fields('revision', [
        $revision_id_field,
        $id_field,
    ])
        ->condition("revision.{$workspace_field}", $workspace_candidates, 'IN')
        ->where("[revision].[{$revision_id_field}] >= [base].[{$revision_id_field}]")
        ->orderBy("revision.{$revision_id_field}", 'ASC');
    // Restrict the result to a set of entity ID's if provided.
    if ($entity_ids) {
        $query->condition("revision.{$id_field}", $entity_ids, 'IN');
    }
    $result = $query->execute()
        ->fetchAllKeyed();
    // Cache the list of associated entity IDs if the full list was requested.
    if (!$entity_ids) {
        $this->associatedRevisions[$workspace_id][$entity_type_id] = $result;
    }
    return $result;
}

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