diff --git a/core/modules/workspace/src/WorkspaceManager.php b/core/modules/workspace/src/WorkspaceManager.php index 9e6bb871f7..ecfc336a09 100644 --- a/core/modules/workspace/src/WorkspaceManager.php +++ b/core/modules/workspace/src/WorkspaceManager.php @@ -4,13 +4,10 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityPublishedInterface; -use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; -use Drupal\workspace\Entity\ContentWorkspace; -use Drupal\workspace\Entity\ContentWorkspaceInterface; use Drupal\workspace\Entity\WorkspaceInterface; use Drupal\workspace\Negotiator\WorkspaceNegotiatorInterface; use Psr\Log\LoggerInterface; @@ -167,46 +164,6 @@ public function setActiveWorkspace(WorkspaceInterface $workspace) { return $this; } - /** - * {@inheritdoc} - */ - public function updateOrCreateFromEntity(EntityInterface $entity) { - if (!$this->entityCanBelongToWorkspaces($entity)) { - return; - } - - // If the entity is not new there should be a ContentWorkspace entry for it. - if (!$entity->isNew()) { - $content_workspaces = $this->entityTypeManager - ->getStorage('content_workspace') - ->loadByProperties([ - 'content_entity_type_id' => $entity->getEntityTypeId(), - 'content_entity_id' => $entity->id(), - ]); - - /** @var \Drupal\workspace\Entity\ContentWorkspaceInterface $content_workspace */ - $content_workspace = reset($content_workspaces); - } - - // If there was a ContentWorkspace entry create a new revision, otherwise - // create a new entity with the type and ID. - if (!empty($content_workspace) && $content_workspace instanceof ContentWorkspaceInterface) { - $content_workspace->setNewRevision(TRUE); - } - else { - $content_workspace = ContentWorkspace::create([ - 'content_entity_type_id' => $entity->getEntityTypeId(), - 'content_entity_id' => $entity->id() - ]); - } - - // Add the revision ID, workspace, and publishing status. - $content_workspace->set('content_entity_revision_id', $entity->getRevisionId()); - $content_workspace->set('workspace', $this->getActiveWorkspace()); - $entity->initial_published ? $content_workspace->setPublished() : $content_workspace->setUnpublished(); - ContentWorkspace::updateOrCreateFromEntity($content_workspace); - } - /** * @return \Drupal\workspace\Negotiator\WorkspaceNegotiatorInterface[] */ diff --git a/core/modules/workspace/src/WorkspaceManagerInterface.php b/core/modules/workspace/src/WorkspaceManagerInterface.php index fd7fc86956..35ea690eb2 100644 --- a/core/modules/workspace/src/WorkspaceManagerInterface.php +++ b/core/modules/workspace/src/WorkspaceManagerInterface.php @@ -57,12 +57,4 @@ public function getActiveWorkspace($object = FALSE); */ public function setActiveWorkspace(WorkspaceInterface $workspace); - /** - * Update or create a ContentWorkspace entity from another entity. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity to update or create from. - */ - public function updateOrCreateFromEntity(EntityInterface $entity); - } diff --git a/core/modules/workspace/workspace.module b/core/modules/workspace/workspace.module index 5683157088..e77e421dac 100644 --- a/core/modules/workspace/workspace.module +++ b/core/modules/workspace/workspace.module @@ -40,12 +40,9 @@ function workspace_help($route_name, RouteMatchInterface $route_match) { */ function workspace_entity_base_field_info(EntityTypeInterface $entity_type) { if (\Drupal::service('workspace.manager')->entityTypeCanBelongToWorkspaces($entity_type)) { - return ['workspace' => BaseFieldDefinition::create('entity_reference') + return ['workspace' => BaseFieldDefinition::create('workspace_reference') ->setLabel(t('Workspace')) ->setDescription(t('The Workspace of this piece of content.')) - ->setComputed(TRUE) - ->setClass(WorkspaceFieldItemList::class) - ->setSetting('target_type', 'workspace') ->setTranslatable(TRUE)]; } } @@ -64,11 +61,7 @@ function workspace_query_entity_query_alter(AlterableInterface $query) { $entity_type = \Drupal::entityTypeManager()->getDefinition($query->getMetaData('entity_type')); if (!empty($entity_type) && $workspace_manager->entityTypeCanBelongToWorkspaces($entity_type)) { - $entity_type_id = $entity_type->id(); - $entity_type_id_key = $entity_type->getKey('id'); - $query->leftJoin('content_workspace_field_revision', 'cwfr', 'base_table.' . $entity_type_id_key . ' = cwfr.content_entity_id'); - $query->condition('cwfr.content_entity_type_id', $entity_type_id); - $query->condition('cwfr.workspace', [$active_workspace, $default_workspace_id], 'IN'); + $query->condition('workspace__target_id', [$active_workspace, $default_workspace_id], 'IN'); } } @@ -89,36 +82,21 @@ function workspace_views_query_alter(ViewExecutable $view, QueryPluginBase $quer return; } - $configuration = [ - 'table' => 'content_workspace_field_revision', - 'field' => 'content_entity_id', - 'left_table' => $entity_type->getDataTable(), - 'left_field' => $entity_type->getKey('id'), - 'operator' => '=', - ]; - /** @var \Drupal\views\Plugin\views\join\JoinPluginBase $join */ - $join = Views::pluginManager('join') - ->createInstance('standard', $configuration); - /** @var \Drupal\views\Plugin\views\query\Sql $query */ - $query->addRelationship('cwrf', $join, 'content_workspace_field_revision'); $query->setWhereGroup('OR', 'workspace'); - $query->addWhere('workspace', 'cwrf.workspace', [$active_workspace, $default_workspace_id], 'IN'); - $query->addWhere('workspace', 'cwrf.workspace', NULL, 'IS'); + $query->addWhere('workspace', 'workspace__target_id', [$active_workspace, $default_workspace_id], 'IN'); + $query->addWhere('workspace', 'workspace__target_id', NULL, 'IS'); foreach ($query->where as $where_id => $where) { foreach ($where['conditions'] as $condition_id => $condition) { if ($condition['field'] == $entity_type->getDataTable() . '.' . $entity_type->getKey('published')) { $value = $query->where[$where_id]['conditions'][$condition_id]['value']; - $query->setWhereGroup('OR', 'published'); - $query->addWhere('published', 'cwrf.published', $value); + $query->setWhereGroup('OR', 'published'); + $query->addWhere('published', 'workspace__status', $value); $query->addWhere('published', $entity_type->getDataTable() . '.' . $entity_type->getKey('published'), $value); unset($query->where[$where_id]['conditions'][$condition_id]); } } } - foreach ($view->displayHandlers->getInstanceIds() as $display_id) { - $view->displayHandlers->get($display_id)->setOption('group_by', TRUE); - } } /** @@ -127,7 +105,8 @@ function workspace_views_query_alter(ViewExecutable $view, QueryPluginBase $quer function workspace_entity_load(array &$entities, $entity_type_id) { /** @var \Drupal\workspace\WorkspaceManagerInterface $workspace_manager */ $workspace_manager = \Drupal::service('workspace.manager'); - if (!$workspace_manager->entityTypeCanBelongToWorkspaces(\Drupal::entityTypeManager()->getDefinition($entity_type_id))) { + $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id); + if (!$workspace_manager->entityTypeCanBelongToWorkspaces($entity_type)) { return; } @@ -139,28 +118,23 @@ function workspace_entity_load(array &$entities, $entity_type_id) { $keys = array_keys($entities); $results = \Drupal::entityTypeManager() - ->getStorage('content_workspace') + ->getStorage($entity_type_id) ->getQuery() ->allRevisions() - ->condition('content_entity_type_id', $entity_type_id) - ->condition('content_entity_id', $keys, 'IN') - ->condition('workspace', [$active_workspace, $default_workspace_id], 'IN') - ->sort('revision_id', 'DESC') + ->condition($entity_type->getKey('id'), $keys, 'IN') + ->condition('workspace.target_id', [$active_workspace, $default_workspace_id], 'IN') + ->sort($entity_type->getKey('revision'), 'DESC') ->range(0, 1) ->execute(); foreach ($results as $revision_id => $entity_id) { - /** @var \Drupal\workspace\Entity\ContentWorkspaceInterface $content_workspace */ - $content_workspace = \Drupal::entityTypeManager() - ->getStorage('content_workspace') - ->loadRevision($revision_id); - $entity = $entities[$content_workspace->get('content_entity_id')->value]; - if ($content_workspace->get('content_entity_revision_id')->value != $entity->getRevisionId()) { + $entity = $entities[$entity_id]; + if ($revision_id != $entity->getRevisionId()) { $new_entity = \Drupal::entityTypeManager() ->getStorage($entity_type_id) - ->loadRevision($content_workspace->get('content_entity_revision_id')->value); - $entities[$entity->id()] = $new_entity; + ->loadRevision($revision_id); + $entities[$entity_id] = $new_entity; } - $content_workspace->isPublished() ? $entities[$entity->id()]->setPublished() : $entities[$entity->id()]->setUnpublished(); + $entities[$entity_id]->workspace->status ? $entities[$entity_id]->setPublished() : $entities[$entity_id]->setUnpublished(); } } @@ -236,34 +210,6 @@ function workspace_entity_presave(EntityInterface $entity) { } } -/** - * Implements hook_entity_insert(). - */ -function workspace_entity_insert(EntityInterface $entity) { - /** @var \Drupal\workspace\WorkspaceManagerInterface $workspace_manager */ - $workspace_manager = \Drupal::service('workspace.manager'); - $workspace_manager->updateOrCreateFromEntity($entity); - if ($workspace_manager->entityCanBelongToWorkspaces($entity)) { - \Drupal::service('workspace.index.sequence') - ->useWorkspace($workspace_manager->getActiveWorkspace()) - ->add($entity); - } -} - -/** - * Implements hook_entity_update(). - */ -function workspace_entity_update(EntityInterface $entity) { - /** @var \Drupal\workspace\WorkspaceManagerInterface $workspace_manager */ - $workspace_manager = \Drupal::service('workspace.manager'); - $workspace_manager->updateOrCreateFromEntity($entity); - if ($workspace_manager->entityCanBelongToWorkspaces($entity)) { - \Drupal::service('workspace.index.sequence') - ->useWorkspace($workspace_manager->getActiveWorkspace()) - ->add($entity); - } -} - /** * Implements hook_theme(). */