annotate core/modules/workspaces/src/EntityQuery/QueryTrait.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\Database\Connection;
Chris@4 6 use Drupal\Core\Entity\EntityTypeInterface;
Chris@4 7 use Drupal\workspaces\WorkspaceManagerInterface;
Chris@4 8
Chris@4 9 /**
Chris@4 10 * Provides workspaces-specific helpers for altering entity queries.
Chris@4 11 */
Chris@4 12 trait QueryTrait {
Chris@4 13
Chris@4 14 /**
Chris@4 15 * The workspace manager.
Chris@4 16 *
Chris@4 17 * @var \Drupal\workspaces\WorkspaceManagerInterface
Chris@4 18 */
Chris@4 19 protected $workspaceManager;
Chris@4 20
Chris@4 21 /**
Chris@4 22 * Constructs a Query object.
Chris@4 23 *
Chris@4 24 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
Chris@4 25 * The entity type definition.
Chris@4 26 * @param string $conjunction
Chris@4 27 * - AND: all of the conditions on the query need to match.
Chris@4 28 * - OR: at least one of the conditions on the query need to match.
Chris@4 29 * @param \Drupal\Core\Database\Connection $connection
Chris@4 30 * The database connection to run the query against.
Chris@4 31 * @param array $namespaces
Chris@4 32 * List of potential namespaces of the classes belonging to this query.
Chris@4 33 * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
Chris@4 34 * The workspace manager.
Chris@4 35 */
Chris@4 36 public function __construct(EntityTypeInterface $entity_type, $conjunction, Connection $connection, array $namespaces, WorkspaceManagerInterface $workspace_manager) {
Chris@4 37 parent::__construct($entity_type, $conjunction, $connection, $namespaces);
Chris@4 38
Chris@4 39 $this->workspaceManager = $workspace_manager;
Chris@4 40 }
Chris@4 41
Chris@4 42 /**
Chris@4 43 * {@inheritdoc}
Chris@4 44 */
Chris@4 45 public function prepare() {
Chris@4 46 parent::prepare();
Chris@4 47
Chris@4 48 // Do not alter entity revision queries.
Chris@4 49 // @todo How about queries for the latest revision? Should we alter them to
Chris@4 50 // look for the latest workspace-specific revision?
Chris@4 51 if ($this->allRevisions) {
Chris@4 52 return $this;
Chris@4 53 }
Chris@4 54
Chris@4 55 // Only alter the query if the active workspace is not the default one and
Chris@4 56 // the entity type is supported.
Chris@4 57 $active_workspace = $this->workspaceManager->getActiveWorkspace();
Chris@4 58 if (!$active_workspace->isDefaultWorkspace() && $this->workspaceManager->isEntityTypeSupported($this->entityType)) {
Chris@4 59 $this->sqlQuery->addMetaData('active_workspace_id', $active_workspace->id());
Chris@4 60 $this->sqlQuery->addMetaData('simple_query', FALSE);
Chris@4 61
Chris@4 62 // LEFT JOIN 'workspace_association' to the base table of the query so we
Chris@4 63 // can properly include live content along with a possible workspace
Chris@4 64 // revision.
Chris@4 65 $id_field = $this->entityType->getKey('id');
Chris@4 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()}'");
Chris@4 67 }
Chris@4 68
Chris@4 69 return $this;
Chris@4 70 }
Chris@4 71
Chris@5 72 /**
Chris@5 73 * {@inheritdoc}
Chris@5 74 */
Chris@5 75 public function isSimpleQuery() {
Chris@5 76 // We declare that this is not a simple query in
Chris@5 77 // \Drupal\workspaces\EntityQuery\QueryTrait::prepare(), but that's not
Chris@5 78 // enough because the parent method can return TRUE in some circumstances.
Chris@5 79 if ($this->sqlQuery->getMetaData('active_workspace_id')) {
Chris@5 80 return FALSE;
Chris@5 81 }
Chris@5 82
Chris@5 83 return parent::isSimpleQuery();
Chris@5 84 }
Chris@5 85
Chris@4 86 }