QueryTrait.php in Multiversion 8.2
File
src/Entity/Query/QueryTrait.php
View source
<?php
namespace Drupal\multiversion\Entity\Query;
use Drupal\multiversion\Entity\Storage\ContentEntityStorageInterface;
use Drupal\workspaces\Entity\Workspace;
trait QueryTrait {
protected $workspaceId = NULL;
protected $isDeleted = FALSE;
public function useWorkspace($id) {
$this->workspaceId = $id;
return $this;
}
public function isDeleted() {
$this->isDeleted = TRUE;
return $this;
}
public function isNotDeleted() {
$this->isDeleted = FALSE;
return $this;
}
public function prepare() {
parent::prepare();
$entity_type = $this->entityTypeManager
->getDefinition($this->entityTypeId);
$enabled = $this->multiversionManager
->isEnabledEntityType($entity_type);
if (is_subclass_of($entity_type
->getStorageClass(), ContentEntityStorageInterface::class) && $enabled) {
$revision_key = $entity_type
->getKey('revision');
$revision_query = FALSE;
foreach ($this->condition
->conditions() as $condition) {
if ($condition['field'] == $revision_key) {
$revision_query = TRUE;
}
}
$workspace = Workspace::load($this
->getWorkspaceId());
if (!$workspace
->isDefaultWorkspace()) {
$this->sqlQuery
->addMetaData('active_workspace_id', $workspace
->id());
$this->sqlQuery
->addMetaData('simple_query', FALSE);
$id_key = $entity_type
->getKey('id');
$workspace_association_table = 'workspace_association';
$this->sqlQuery
->leftJoin($workspace_association_table, $workspace_association_table, "%alias.target_entity_type_id = '{$this->entityTypeId}' AND %alias.target_entity_id = base_table.{$id_key}");
$this->sqlQuery
->condition($this->sqlQuery
->orConditionGroup()
->condition("{$workspace_association_table}.workspace", $workspace
->id())
->condition("{$workspace_association_table}.workspace", NULL, 'IS'));
}
if (!$revision_query) {
$this
->condition('_deleted', (int) $this->isDeleted);
}
}
return $this;
}
protected function getWorkspaceId() {
if ($this->workspaceId) {
return $this->workspaceId;
}
if ($workspace = \Drupal::service('workspaces.manager')
->getActiveWorkspace()) {
return $workspace
->id();
}
return NULL;
}
}
Traits
Name |
Description |
QueryTrait |
@property $entityTypeId
@property $entityTypeManager
@property $condition |