function WorkspaceAssociation::getAssociatedInitialRevisions

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

Retrieves all content revisions that were created in a given workspace.

Parameters

string $workspace_id: The ID of the workspace.

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

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

Return value

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

Overrides WorkspaceAssociationInterface::getAssociatedInitialRevisions

File

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

Class

WorkspaceAssociation
Provides a class for CRUD operations on workspace associations.

Namespace

Drupal\workspaces

Code

public function getAssociatedInitialRevisions(string $workspace_id, string $entity_type_id, array $entity_ids = []) {
  if (isset($this->associatedInitialRevisions[$workspace_id][$entity_type_id])) {
    if ($entity_ids) {
      return array_intersect($this->associatedInitialRevisions[$workspace_id][$entity_type_id], $entity_ids);
    }
    else {
      return $this->associatedInitialRevisions[$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'];
  $query = $this->database
    ->select($entity_type->getBaseTable(), 'base');
  $query->leftJoin($entity_type->getRevisionTable(), 'revision', "[base].[{$revision_id_field}] = [revision].[{$revision_id_field}]");
  $query->fields('base', [
    $revision_id_field,
    $id_field,
  ])
    ->condition("revision.{$workspace_field}", $workspace_id, '=')
    ->orderBy("base.{$revision_id_field}", 'ASC');
  // Restrict the result to a set of entity ID's if provided.
  if ($entity_ids) {
    $query->condition("base.{$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->associatedInitialRevisions[$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.