diff --git a/core/modules/workspaces/src/WorkspacesEntityRepository.php b/core/modules/workspaces/src/WorkspacesEntityRepository.php new file mode 100644 index 0000000000000000000000000000000000000000..b15a7f5fa4244c5f2dfdc54b2828278619ac204c --- /dev/null +++ b/core/modules/workspaces/src/WorkspacesEntityRepository.php @@ -0,0 +1,77 @@ +<?php + +namespace Drupal\workspaces; + +use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityRepositoryInterface; + +/** + * Provides workspace-specific mechanisms for retrieving entities. + */ +class WorkspacesEntityRepository implements EntityRepositoryInterface { + + public function __construct( + protected EntityRepositoryInterface $inner, + protected WorkspaceManagerInterface $workspaceManager, + ) {} + + /** + * {@inheritdoc} + */ + public function loadEntityByUuid($entity_type_id, $uuid) { + return $this->inner->loadEntityByUuid($entity_type_id, $uuid); + } + + /** + * {@inheritdoc} + */ + public function loadEntityByConfigTarget($entity_type_id, $target) { + return $this->inner->loadEntityByConfigTarget($entity_type_id, $target); + } + + /** + * {@inheritdoc} + */ + public function getTranslationFromContext(EntityInterface $entity, $langcode = NULL, $context = []) { + return $this->inner->getTranslationFromContext($entity, $langcode, $context); + } + + /** + * {@inheritdoc} + */ + public function getActive($entity_type_id, $entity_id, ?array $contexts = NULL) { + // When there's no active workspace, the active entity variant is the + // canonical one. + if (!$this->workspaceManager->hasActiveWorkspace()) { + return $this->inner->getCanonical($entity_type_id, $entity_id, $contexts); + } + return $this->inner->getActive($entity_type_id, $entity_id, $contexts); + } + + /** + * {@inheritdoc} + */ + public function getActiveMultiple($entity_type_id, array $entity_ids, ?array $contexts = NULL) { + // When there's no active workspace, the active entity variant is the + // canonical one. + if (!$this->workspaceManager->hasActiveWorkspace()) { + return $this->inner->getCanonicalMultiple($entity_type_id, $entity_ids, $contexts); + } + return $this->inner->getActiveMultiple($entity_type_id, $entity_ids, $contexts); + } + + /** + * {@inheritdoc} + */ + public function getCanonical($entity_type_id, $entity_id, ?array $contexts = NULL) { + return $this->inner->getCanonical($entity_type_id, $entity_id, $contexts); + } + + /** + * {@inheritdoc} + */ + public function getCanonicalMultiple($entity_type_id, array $entity_ids, ?array $contexts = NULL) { + return $this->inner->getCanonicalMultiple($entity_type_id, $entity_ids, $contexts); + } + +} diff --git a/core/modules/workspaces/tests/src/Kernel/WorkspaceEntityRepositoryTest.php b/core/modules/workspaces/tests/src/Kernel/WorkspaceEntityRepositoryTest.php index cbee6e4b7d632ed5e11d7dcb654766180cde2eb5..956d6f55b5dbe4e6ef024910ff9aef9b74b03c2a 100644 --- a/core/modules/workspaces/tests/src/Kernel/WorkspaceEntityRepositoryTest.php +++ b/core/modules/workspaces/tests/src/Kernel/WorkspaceEntityRepositoryTest.php @@ -91,6 +91,11 @@ public function testGetActive(): void { $active = $this->entityRepository->getActive($entity_type_id, $entity->id(), $en_contexts); $this->assertSame($ham_revision->getLoadedRevisionId(), $active->getLoadedRevisionId()); + // Check that the active variant in Live is still the default revision. + $this->switchToLive(); + $active = $this->entityRepository->getActive($entity_type_id, $entity->id(), $en_contexts); + $this->assertSame($entity->getLoadedRevisionId(), $active->getLoadedRevisionId()); + $this->switchToWorkspace('cheese'); $cheese_revision = $storage->createRevision($entity); $storage->save($cheese_revision); diff --git a/core/modules/workspaces/workspaces.services.yml b/core/modules/workspaces/workspaces.services.yml index f124d5e376c3f745d800dfc2770968d0d1dbe96f..1b1575da07e575098af18cae4e911c93238d66aa 100644 --- a/core/modules/workspaces/workspaces.services.yml +++ b/core/modules/workspaces/workspaces.services.yml @@ -64,6 +64,12 @@ services: arguments: [ '@workspaces.manager', '@plugin.manager.element_info' ] Drupal\workspaces\WorkspacesLazyBuilders: '@workspaces.lazy_builders' + workspaces.entity.repository: + decorates: entity.repository + class: Drupal\workspaces\WorkspacesEntityRepository + arguments: ['@.inner', '@workspaces.manager'] + public: false + workspaces.entity.query.sql: decorates: entity.query.sql class: Drupal\workspaces\EntityQuery\QueryFactory