View source
<?php
namespace Drupal\workspaces\EntityQuery;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\EntityType;
use Drupal\Core\Entity\Query\Sql\Tables as BaseTables;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
class Tables extends BaseTables {
protected $workspaceManager;
protected $contentWorkspaceTables = [];
protected $baseTablesEntityType = [];
public function __construct(SelectInterface $sql_query) {
parent::__construct($sql_query);
$this->workspaceManager = \Drupal::service('workspaces.manager');
if ($this->sqlQuery
->getMetaData('active_workspace_id')) {
$this->contentWorkspaceTables['base_table'] = 'workspace_association';
$this->baseTablesEntityType['base_table'] = $this->sqlQuery
->getMetaData('entity_type');
}
}
public function addField($field, $type, $langcode) {
if ($this->sqlQuery
->getMetaData('active_workspace_id')) {
$previous_all_revisions = $this->sqlQuery
->getMetaData('all_revisions');
$this->sqlQuery
->addMetaData('all_revisions', TRUE);
}
$alias = parent::addField($field, $type, $langcode);
if (isset($previous_all_revisions)) {
$this->sqlQuery
->addMetaData('all_revisions', $previous_all_revisions);
}
return $alias;
}
protected function addJoin($type, $table, $join_condition, $langcode, $delta = NULL) {
if ($this->sqlQuery
->getMetaData('active_workspace_id')) {
$condition_parts = explode(' = ', $join_condition);
$condition_parts_1 = str_replace([
'[',
']',
], '', $condition_parts[1]);
[
$base_table,
$id_field,
] = explode('.', $condition_parts_1);
if (isset($this->baseTablesEntityType[$base_table])) {
$entity_type_id = $this->baseTablesEntityType[$base_table];
$revision_key = $this->entityTypeManager
->getActiveDefinition($entity_type_id)
->getKey('revision');
if ($id_field === $revision_key || $id_field === 'revision_id') {
$workspace_association_table = $this->contentWorkspaceTables[$base_table];
$join_condition = "{$condition_parts[0]} = COALESCE({$workspace_association_table}.target_entity_revision_id, {$condition_parts[1]})";
}
}
}
return parent::addJoin($type, $table, $join_condition, $langcode, $delta);
}
protected function addNextBaseTable(EntityType $entity_type, $table, $sql_column, FieldStorageDefinitionInterface $field_storage) {
$next_base_table_alias = parent::addNextBaseTable($entity_type, $table, $sql_column, $field_storage);
$active_workspace_id = $this->sqlQuery
->getMetaData('active_workspace_id');
if ($active_workspace_id && $this->workspaceManager
->isEntityTypeSupported($entity_type)) {
$this
->addWorkspaceAssociationJoin($entity_type
->id(), $next_base_table_alias, $active_workspace_id);
}
return $next_base_table_alias;
}
public function addWorkspaceAssociationJoin($entity_type_id, $base_table_alias, $active_workspace_id) {
if (!isset($this->contentWorkspaceTables[$base_table_alias])) {
$entity_type = $this->entityTypeManager
->getActiveDefinition($entity_type_id);
$id_field = $entity_type
->getKey('id');
$this->contentWorkspaceTables[$base_table_alias] = $this->sqlQuery
->leftJoin('workspace_association', NULL, "[%alias].[target_entity_type_id] = '{$entity_type_id}' AND [%alias].[target_entity_id] = [{$base_table_alias}].[{$id_field}] AND [%alias].[workspace] = '{$active_workspace_id}'");
$this->baseTablesEntityType[$base_table_alias] = $entity_type
->id();
}
return $this->contentWorkspaceTables[$base_table_alias];
}
}