function QueryTrait::prepare

Same name in other branches
  1. 9 core/modules/workspaces/src/EntityQuery/QueryTrait.php \Drupal\workspaces\EntityQuery\QueryTrait::prepare()
  2. 8.9.x core/modules/workspaces/src/EntityQuery/QueryTrait.php \Drupal\workspaces\EntityQuery\QueryTrait::prepare()
  3. 10 core/modules/workspaces/src/EntityQuery/QueryTrait.php \Drupal\workspaces\EntityQuery\QueryTrait::prepare()
2 calls to QueryTrait::prepare()
Query::prepare in core/modules/workspaces/src/EntityQuery/Query.php
Prepares the basic query with proper metadata/tags and base fields.
QueryAggregate::prepare in core/modules/workspaces/src/EntityQuery/QueryAggregate.php
Prepares the basic query with proper metadata/tags and base fields.

File

core/modules/workspaces/src/EntityQuery/QueryTrait.php, line 59

Class

QueryTrait
Provides workspaces-specific helpers for altering entity queries.

Namespace

Drupal\workspaces\EntityQuery

Code

public function prepare() {
    // Latest revision queries have to return the latest workspace-specific
    // revisions, in order to prevent changes done outside the workspace from
    // leaking into the currently active one. For the same reason, latest
    // revision queries will return the default revision for entities that are
    // not tracked in the active workspace.
    if ($this->latestRevision && $this->workspaceInfo
        ->isEntityTypeSupported($this->entityType) && $this->workspaceManager
        ->hasActiveWorkspace()) {
        $this->allRevisions = FALSE;
        $this->latestRevision = FALSE;
    }
    parent::prepare();
    // Do not alter entity revision queries.
    if ($this->allRevisions) {
        return $this;
    }
    // Only alter the query if the active workspace is not the default one and
    // the entity type is supported.
    if ($this->workspaceInfo
        ->isEntityTypeSupported($this->entityType) && $this->workspaceManager
        ->hasActiveWorkspace()) {
        $active_workspace = $this->workspaceManager
            ->getActiveWorkspace();
        $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');
        $target_id_field = WorkspaceAssociation::getIdField($this->entityTypeId);
        $this->sqlQuery
            ->leftJoin('workspace_association', 'workspace_association', "[%alias].[target_entity_type_id] = '{$this->entityTypeId}' AND [%alias].[{$target_id_field}] = [base_table].[{$id_field}] AND [%alias].[workspace] = '{$active_workspace->id()}'");
    }
    return $this;
}

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