You are here

public function QueryTrait::prepare in Workspace 8.2

2 calls to QueryTrait::prepare()
Query::prepare in src/EntityQuery/Query.php
Prepares the basic query with proper metadata/tags and base fields.
QueryAggregate::prepare in src/EntityQuery/QueryAggregate.php
Prepares the basic query with proper metadata/tags and base fields.

File

src/EntityQuery/QueryTrait.php, line 45

Class

QueryTrait
Provides workspace-specific helpers for altering entity queries.

Namespace

Drupal\workspace\EntityQuery

Code

public function prepare() {
  parent::prepare();

  // Do not alter entity revision queries.
  // @todo How about queries for the latest revision? Should we alter them to
  //   look for the latest workspace-specific revision?
  if ($this->allRevisions) {
    return $this;
  }

  // Only alter the query if the active workspace is not the default one and
  // the entity type is supported.
  $active_workspace = $this->workspaceManager
    ->getActiveWorkspace();
  if (!$active_workspace
    ->isDefaultWorkspace() && $this->workspaceManager
    ->isEntityTypeSupported($this->entityType)) {
    $this->sqlQuery
      ->addMetaData('active_workspace_id', $active_workspace
      ->id());
    $this->sqlQuery
      ->addMetaData('simple_query', FALSE);

    // LEFT JOIN 'workspace_association' to the base table of the query so we
    // can properly include live content along with a possible workspace
    // revision.
    $id_field = $this->entityType
      ->getKey('id');
    $this->sqlQuery
      ->leftJoin('workspace_association', 'workspace_association', "%alias.target_entity_type_id = '{$this->entityTypeId}' AND %alias.target_entity_id = base_table.{$id_field} AND %alias.workspace = '{$active_workspace->id()}'");
  }
  return $this;
}