annotate core/modules/workspaces/src/EntityQuery/Query.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@17 1 <?php
Chris@17 2
Chris@17 3 namespace Drupal\workspaces\EntityQuery;
Chris@17 4
Chris@17 5 use Drupal\Core\Entity\Query\Sql\Query as BaseQuery;
Chris@17 6
Chris@17 7 /**
Chris@17 8 * Alters entity queries to use a workspace revision instead of the default one.
Chris@17 9 */
Chris@17 10 class Query extends BaseQuery {
Chris@17 11
Chris@17 12 use QueryTrait {
Chris@17 13 prepare as traitPrepare;
Chris@17 14 }
Chris@17 15
Chris@17 16 /**
Chris@17 17 * {@inheritdoc}
Chris@17 18 */
Chris@17 19 public function prepare() {
Chris@17 20 $this->traitPrepare();
Chris@17 21
Chris@17 22 // If the prepare() method from the trait decided that we need to alter this
Chris@17 23 // query, we need to re-define the the key fields for fetchAllKeyed() as SQL
Chris@17 24 // expressions.
Chris@17 25 if ($this->sqlQuery->getMetaData('active_workspace_id')) {
Chris@17 26 $id_field = $this->entityType->getKey('id');
Chris@17 27 $revision_field = $this->entityType->getKey('revision');
Chris@17 28
Chris@17 29 // Since the query is against the base table, we have to take into account
Chris@17 30 // that the revision ID might come from the workspace_association
Chris@17 31 // relationship, and, as a consequence, the revision ID field is no longer
Chris@17 32 // a simple SQL field but an expression.
Chris@17 33 $this->sqlFields = [];
Chris@18 34 $this->sqlQuery->addExpression("COALESCE(workspace_association.target_entity_revision_id, base_table.$revision_field)", $revision_field);
Chris@18 35 $this->sqlQuery->addExpression("base_table.$id_field", $id_field);
Chris@18 36
Chris@18 37 $this->sqlGroupBy['workspace_association.target_entity_revision_id'] = 'workspace_association.target_entity_revision_id';
Chris@18 38 $this->sqlGroupBy["base_table.$id_field"] = "base_table.$id_field";
Chris@18 39 $this->sqlGroupBy["base_table.$revision_field"] = "base_table.$revision_field";
Chris@17 40 }
Chris@17 41
Chris@17 42 return $this;
Chris@17 43 }
Chris@17 44
Chris@17 45 }