You are here

public function Query::prepare in Workspace 8.2

Prepares the basic query with proper metadata/tags and base fields.

Return value

$this Returns the called object.

Throws

\Drupal\Core\Entity\Query\QueryException Thrown if the base table does not exist.

Overrides Query::prepare

File

src/EntityQuery/Query.php, line 30

Class

Query
Alters entity queries to use a workspace revision instead of the default one.

Namespace

Drupal\workspace\EntityQuery

Code

public function prepare() {
  $this
    ->traitPrepare();

  // If the prepare() method from the trait decided that we need to alter this
  // query, we need to re-define the the key fields for fetchAllKeyed() as SQL
  // expressions.
  if ($this->sqlQuery
    ->getMetaData('active_workspace_id')) {
    $id_field = $this->entityType
      ->getKey('id');
    $revision_field = $this->entityType
      ->getKey('revision');

    // Since the query is against the base table, we have to take into account
    // that the revision ID might come from the workspace_association
    // relationship, and, as a consequence, the revision ID field is no longer
    // a simple SQL field but an expression.
    $this->sqlFields = [];
    $this->sqlExpressions[$revision_field] = "COALESCE(workspace_association.target_entity_revision_id, base_table.{$revision_field})";
    $this->sqlExpressions[$id_field] = "base_table.{$id_field}";
  }
  return $this;
}