You are here

public function WorkspaceAssociation::getAssociatedRevisions in Drupal 9

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

Retrieves all content revisions tracked by a given workspace.

Since the 'workspace_association' index table only tracks the latest associated revisions, this method retrieves all the tracked revisions by querying the entity type's revision table directly.

Parameters

string $workspace_id: The ID of the workspace.

string $entity_type_id: An entity type ID to find revisions for.

int[]|string[]|null $entity_ids: (optional) An array of entity IDs to filter the results by. Defaults to NULL.

Return value

array Returns an array where the values are an array of entity IDs keyed by revision IDs.

Overrides WorkspaceAssociationInterface::getAssociatedRevisions

File

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

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) {

  /** @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'];
  $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_id)
    ->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');
  }
  return $query
    ->execute()
    ->fetchAllKeyed();
}