annotate core/modules/workspaces/src/EntityQuery/Query.php @ 5:12f9dff5fda9 tip

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