From 9a1a9ed2bd54c73aaab65b301b0098f4273ce12c Mon Sep 17 00:00:00 2001
From: catch <6915-catch@users.noreply.drupalcode.org>
Date: Tue, 11 Feb 2025 13:23:06 +0000
Subject: [PATCH] Issue #3504057 by amateescu, smustgrave: The active variant
 of an entity in the Live workspace should be the default revision

---
 .../src/WorkspacesEntityRepository.php        | 77 +++++++++++++++++++
 .../Kernel/WorkspaceEntityRepositoryTest.php  |  5 ++
 .../workspaces/workspaces.services.yml        |  6 ++
 3 files changed, 88 insertions(+)
 create mode 100644 core/modules/workspaces/src/WorkspacesEntityRepository.php

diff --git a/core/modules/workspaces/src/WorkspacesEntityRepository.php b/core/modules/workspaces/src/WorkspacesEntityRepository.php
new file mode 100644
index 000000000000..b15a7f5fa424
--- /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 cbee6e4b7d63..956d6f55b5db 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 f124d5e376c3..1b1575da07e5 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
-- 
GitLab