annotate core/modules/workspaces/src/EntityQuery/QueryTrait.php @ 19:fa3358dc1485 tip

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