3 namespace Drupal\workspaces\EntityQuery;
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Entity\EntityTypeInterface;
7 use Drupal\workspaces\WorkspaceManagerInterface;
10 * Provides workspaces-specific helpers for altering entity queries.
15 * The workspace manager.
17 * @var \Drupal\workspaces\WorkspaceManagerInterface
19 protected $workspaceManager;
22 * Constructs a Query object.
24 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
25 * The entity type definition.
26 * @param string $conjunction
27 * - AND: all of the conditions on the query need to match.
28 * - OR: at least one of the conditions on the query need to match.
29 * @param \Drupal\Core\Database\Connection $connection
30 * The database connection to run the query against.
31 * @param array $namespaces
32 * List of potential namespaces of the classes belonging to this query.
33 * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
34 * The workspace manager.
36 public function __construct(EntityTypeInterface $entity_type, $conjunction, Connection $connection, array $namespaces, WorkspaceManagerInterface $workspace_manager) {
37 parent::__construct($entity_type, $conjunction, $connection, $namespaces);
39 $this->workspaceManager = $workspace_manager;
45 public function prepare() {
48 // Do not alter entity revision queries.
49 // @todo How about queries for the latest revision? Should we alter them to
50 // look for the latest workspace-specific revision?
51 if ($this->allRevisions) {
55 // Only alter the query if the active workspace is not the default one and
56 // the entity type is supported.
57 $active_workspace = $this->workspaceManager->getActiveWorkspace();
58 if (!$active_workspace->isDefaultWorkspace() && $this->workspaceManager->isEntityTypeSupported($this->entityType)) {
59 $this->sqlQuery->addMetaData('active_workspace_id', $active_workspace->id());
60 $this->sqlQuery->addMetaData('simple_query', FALSE);
62 // LEFT JOIN 'workspace_association' to the base table of the query so we
63 // can properly include live content along with a possible workspace
65 $id_field = $this->entityType->getKey('id');
66 $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()}'");