Query.php

Same filename in this branch
  1. 9 core/lib/Drupal/Core/Config/Entity/Query/Query.php
  2. 9 core/lib/Drupal/Core/Database/Query/Query.php
  3. 9 core/lib/Drupal/Core/Entity/Query/Sql/Query.php
  4. 9 core/lib/Drupal/Core/Entity/Query/Null/Query.php
  5. 9 core/lib/Drupal/Core/Entity/KeyValueStore/Query/Query.php
Same filename in other branches
  1. 8.9.x core/modules/workspaces/src/EntityQuery/Query.php
  2. 8.9.x core/lib/Drupal/Core/Config/Entity/Query/Query.php
  3. 8.9.x core/lib/Drupal/Core/Database/Query/Query.php
  4. 8.9.x core/lib/Drupal/Core/Entity/Query/Sql/Query.php
  5. 8.9.x core/lib/Drupal/Core/Entity/Query/Null/Query.php
  6. 8.9.x core/lib/Drupal/Core/Entity/KeyValueStore/Query/Query.php
  7. 10 core/modules/workspaces/src/EntityQuery/Query.php
  8. 10 core/lib/Drupal/Core/Config/Entity/Query/Query.php
  9. 10 core/lib/Drupal/Core/Database/Query/Query.php
  10. 10 core/lib/Drupal/Core/Entity/Query/Sql/Query.php
  11. 10 core/lib/Drupal/Core/Entity/Query/Null/Query.php
  12. 10 core/lib/Drupal/Core/Entity/KeyValueStore/Query/Query.php
  13. 11.x core/modules/workspaces/src/EntityQuery/Query.php
  14. 11.x core/lib/Drupal/Core/Config/Entity/Query/Query.php
  15. 11.x core/lib/Drupal/Core/Database/Query/Query.php
  16. 11.x core/lib/Drupal/Core/Entity/Query/Sql/Query.php
  17. 11.x core/lib/Drupal/Core/Entity/Query/Null/Query.php
  18. 11.x core/lib/Drupal/Core/Entity/KeyValueStore/Query/Query.php

Namespace

Drupal\workspaces\EntityQuery

File

core/modules/workspaces/src/EntityQuery/Query.php

View source
<?php

namespace Drupal\workspaces\EntityQuery;

use Drupal\Core\Entity\Query\Sql\Query as BaseQuery;

/**
 * Alters entity queries to use a workspace revision instead of the default one.
 */
class Query extends BaseQuery {
    use QueryTrait {
        prepare as traitPrepare;
    }
    
    /**
     * {@inheritdoc}
     */
    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 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->sqlQuery
                ->addExpression("COALESCE([workspace_association].[target_entity_revision_id], [base_table].[{$revision_field}])", $revision_field);
            $this->sqlQuery
                ->addExpression("[base_table].[{$id_field}]", $id_field);
            $this->sqlGroupBy['workspace_association.target_entity_revision_id'] = 'workspace_association.target_entity_revision_id';
            $this->sqlGroupBy["base_table.{$id_field}"] = "base_table.{$id_field}";
            $this->sqlGroupBy["base_table.{$revision_field}"] = "base_table.{$revision_field}";
        }
        return $this;
    }

}

Classes

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

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