QueryTrait.php 2.97 KB
Newer Older
1 2
<?php

3
namespace Drupal\workspaces\EntityQuery;
4 5 6

use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeInterface;
7
use Drupal\workspaces\WorkspaceManagerInterface;
8 9

/**
10
 * Provides workspaces-specific helpers for altering entity queries.
11 12 13 14 15 16
 */
trait QueryTrait {

  /**
   * The workspace manager.
   *
17
   * @var \Drupal\workspaces\WorkspaceManagerInterface
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
   */
  protected $workspaceManager;

  /**
   * Constructs a Query object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param string $conjunction
   *   - AND: all of the conditions on the query need to match.
   *   - OR: at least one of the conditions on the query need to match.
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection to run the query against.
   * @param array $namespaces
   *   List of potential namespaces of the classes belonging to this query.
33
   * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
   *   The workspace manager.
   */
  public function __construct(EntityTypeInterface $entity_type, $conjunction, Connection $connection, array $namespaces, WorkspaceManagerInterface $workspace_manager) {
    parent::__construct($entity_type, $conjunction, $connection, $namespaces);

    $this->workspaceManager = $workspace_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function prepare() {
    parent::prepare();

    // Do not alter entity revision queries.
    // @todo How about queries for the latest revision? Should we alter them to
    //   look for the latest workspace-specific revision?
    if ($this->allRevisions) {
      return $this;
    }

    // Only alter the query if the active workspace is not the default one and
    // the entity type is supported.
    $active_workspace = $this->workspaceManager->getActiveWorkspace();
    if (!$active_workspace->isDefaultWorkspace() && $this->workspaceManager->isEntityTypeSupported($this->entityType)) {
      $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');
      $this->sqlQuery->leftJoin('workspace_association', 'workspace_association', "%alias.target_entity_type_id = '{$this->entityTypeId}' AND %alias.target_entity_id = base_table.$id_field AND %alias.workspace = '{$active_workspace->id()}'");
    }

    return $this;
  }

72 73 74 75 76 77 78 79 80 81 82 83 84 85
  /**
   * {@inheritdoc}
   */
  public function isSimpleQuery() {
    // We declare that this is not a simple query in
    // \Drupal\workspaces\EntityQuery\QueryTrait::prepare(), but that's not
    // enough because the parent method can return TRUE in some circumstances.
    if ($this->sqlQuery->getMetaData('active_workspace_id')) {
      return FALSE;
    }

    return parent::isSimpleQuery();
  }

86
}