Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / workspaces / src / EntityQuery / QueryTrait.php
1 <?php
2
3 namespace Drupal\workspaces\EntityQuery;
4
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Entity\EntityTypeInterface;
7 use Drupal\workspaces\WorkspaceManagerInterface;
8
9 /**
10  * Provides workspaces-specific helpers for altering entity queries.
11  */
12 trait QueryTrait {
13
14   /**
15    * The workspace manager.
16    *
17    * @var \Drupal\workspaces\WorkspaceManagerInterface
18    */
19   protected $workspaceManager;
20
21   /**
22    * Constructs a Query object.
23    *
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.
35    */
36   public function __construct(EntityTypeInterface $entity_type, $conjunction, Connection $connection, array $namespaces, WorkspaceManagerInterface $workspace_manager) {
37     parent::__construct($entity_type, $conjunction, $connection, $namespaces);
38
39     $this->workspaceManager = $workspace_manager;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function prepare() {
46     parent::prepare();
47
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) {
52       return $this;
53     }
54
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);
61
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
64       // revision.
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()}'");
67     }
68
69     return $this;
70   }
71
72 }