From 1aee1dda6e10c68e36e0e4e282241f3b424f332f Mon Sep 17 00:00:00 2001
From: Alec Smrekar <alec@smrekar.me>
Date: Wed, 9 Apr 2025 17:40:48 +0200
Subject: [PATCH 1/5] 3518220: Create translation in WS, not live

---
 .../workspaces/src/Hook/EntityOperations.php  | 35 +++++++++++++------
 1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/core/modules/workspaces/src/Hook/EntityOperations.php b/core/modules/workspaces/src/Hook/EntityOperations.php
index c459f4d065e3..ce1f316d8d70 100644
--- a/core/modules/workspaces/src/Hook/EntityOperations.php
+++ b/core/modules/workspaces/src/Hook/EntityOperations.php
@@ -10,8 +10,10 @@
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Entity\Query\QueryInterface;
 use Drupal\Core\Entity\RevisionableInterface;
+use Drupal\Core\Entity\SynchronizableInterface;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Hook\Attribute\Hook;
+use Drupal\Core\TypedData\TranslatableInterface;
 use Drupal\workspaces\WorkspaceAssociationInterface;
 use Drupal\workspaces\WorkspaceInformationInterface;
 use Drupal\workspaces\WorkspaceManagerInterface;
@@ -200,18 +202,29 @@ public function entityTranslationInsert(EntityInterface $translation): void {
     // that translation to the default revision as well, otherwise the new
     // translation wouldn't show up in entity queries or views which use the
     // field data table as the base table.
-    $this->workspaceManager->executeOutsideWorkspace(function () use ($translation) {
-      $storage = $this->entityTypeManager->getStorage($translation->getEntityTypeId());
-      $default_revision = $storage->load($translation->id());
-
-      $langcode = $translation->language()->getId();
-      if (!$default_revision->hasTranslation($langcode)) {
-        $default_revision_translation = $default_revision->addTranslation($langcode, $translation->toArray());
-        $default_revision_translation->setUnpublished();
-        $default_revision_translation->setSyncing(TRUE);
-        $default_revision_translation->save();
-      }
+    $default_revision = $this->workspaceManager->executeOutsideWorkspace(function () use ($translation) {
+      return $this->entityTypeManager
+        ->getStorage($translation->getEntityTypeId())
+        ->load($translation->id());
     });
+
+    if (!$default_revision instanceof TranslatableInterface) {
+      return;
+    }
+    if (!$default_revision instanceof SynchronizableInterface) {
+      return;
+    }
+    if (!$default_revision instanceof EntityPublishedInterface) {
+      return;
+    }
+    $langcode = $translation->language()->getId();
+    if (!$default_revision->hasTranslation($langcode)) {
+      $default_revision_translation = $default_revision->addTranslation($langcode, $translation->toArray());
+      assert($default_revision_translation instanceof EntityPublishedInterface);
+      $default_revision_translation->setUnpublished();
+      $default_revision_translation->setSyncing(TRUE);
+      $default_revision_translation->save();
+    }
   }
 
   /**
-- 
GitLab


From f514347a3f9d0d637a5882289ff12f1db33c632f Mon Sep 17 00:00:00 2001
From: Alec Smrekar <alec@smrekar.me>
Date: Mon, 14 Apr 2025 11:11:14 +0200
Subject: [PATCH 2/5] 3518220: add tests

---
 .../src/Hook/WorkspacesTestHooks.php          | 10 ++++++++
 .../WorkspaceContentTranslationTest.php       | 23 +++++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/core/modules/workspaces/tests/modules/workspaces_test/src/Hook/WorkspacesTestHooks.php b/core/modules/workspaces/tests/modules/workspaces_test/src/Hook/WorkspacesTestHooks.php
index 09ab54054ff6..8c9b3cbf629f 100644
--- a/core/modules/workspaces/tests/modules/workspaces_test/src/Hook/WorkspacesTestHooks.php
+++ b/core/modules/workspaces/tests/modules/workspaces_test/src/Hook/WorkspacesTestHooks.php
@@ -24,4 +24,14 @@ public function entityTypeAlter(array &$entity_types) : void {
     }
   }
 
+  /**
+   * Implements hook_ENTITY_TYPE_translation_create() for 'entity_test_mulrevpub'.
+   */
+  #[Hook('entity_test_mulrevpub_translation_create')]
+  public function entityTranslationCreate(): void {
+    $workspace_manager = \Drupal::service('workspaces.manager');
+    /** @var \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager */
+    \Drupal::state()->set('workspace_was_active', $workspace_manager->hasActiveWorkspace());
+  }
+
 }
diff --git a/core/modules/workspaces/tests/src/Kernel/WorkspaceContentTranslationTest.php b/core/modules/workspaces/tests/src/Kernel/WorkspaceContentTranslationTest.php
index 70351c963187..d7b790bd2425 100644
--- a/core/modules/workspaces/tests/src/Kernel/WorkspaceContentTranslationTest.php
+++ b/core/modules/workspaces/tests/src/Kernel/WorkspaceContentTranslationTest.php
@@ -35,6 +35,7 @@ class WorkspaceContentTranslationTest extends KernelTestBase {
     'language',
     'user',
     'workspaces',
+    'workspaces_test',
   ];
 
   /**
@@ -128,4 +129,26 @@ public function testTranslations(): void {
     $this->assertEquals('live - 2 - unpublished - RO', $translation->get('name')->value);
   }
 
+  /**
+   * Test that the default revision translation is created in a WS.
+   *
+   * @covers \Drupal\workspaces\Hook\EntityOperations::entityTranslationInsert
+   */
+  public function testAddingTranslationToDefaultRevision(): void {
+    $storage = $this->entityTypeManager->getStorage('entity_test_mulrevpub');
+
+    // Create a published entity in Live.
+    $entity_published = $storage->create(['name' => 'test']);
+    $entity_published->save();
+
+    // Add a translation in a WS. The translation insertion should happen in
+    // the WS, not live.
+    $this->switchToWorkspace('stage');
+    $entity_published = $storage->loadUnchanged($entity_published->id());
+    $entity_published->addTranslation('ro', ['name' => 'testAddingTranslationToDefaultRevision']);
+    $entity_published->save();
+
+    $this->assertTrue(\Drupal::state()->get('workspace_was_active'));
+  }
+
 }
-- 
GitLab


From 4d5426f642df172c323c01e08ee584e869c765cd Mon Sep 17 00:00:00 2001
From: Alec Smrekar <alec@smrekar.me>
Date: Mon, 28 Apr 2025 12:13:27 +0200
Subject: [PATCH 3/5] Remove unnecessary test method

---
 .../WorkspaceContentTranslationTest.php       | 25 +++----------------
 1 file changed, 3 insertions(+), 22 deletions(-)

diff --git a/core/modules/workspaces/tests/src/Kernel/WorkspaceContentTranslationTest.php b/core/modules/workspaces/tests/src/Kernel/WorkspaceContentTranslationTest.php
index d7b790bd2425..a31ad20d0adb 100644
--- a/core/modules/workspaces/tests/src/Kernel/WorkspaceContentTranslationTest.php
+++ b/core/modules/workspaces/tests/src/Kernel/WorkspaceContentTranslationTest.php
@@ -85,6 +85,9 @@ public function testTranslations(): void {
     $entity_published->addTranslation('ro', ['name' => 'live - 1 - published - RO']);
     $entity_published->save();
 
+    // Test that the default revision translation is created in a WS.
+    $this->assertTrue(\Drupal::state()->get('workspace_was_active'));
+
     $entity_unpublished->addTranslation('ro', ['name' => 'live - 2 - unpublished - RO']);
     $entity_unpublished->save();
 
@@ -129,26 +132,4 @@ public function testTranslations(): void {
     $this->assertEquals('live - 2 - unpublished - RO', $translation->get('name')->value);
   }
 
-  /**
-   * Test that the default revision translation is created in a WS.
-   *
-   * @covers \Drupal\workspaces\Hook\EntityOperations::entityTranslationInsert
-   */
-  public function testAddingTranslationToDefaultRevision(): void {
-    $storage = $this->entityTypeManager->getStorage('entity_test_mulrevpub');
-
-    // Create a published entity in Live.
-    $entity_published = $storage->create(['name' => 'test']);
-    $entity_published->save();
-
-    // Add a translation in a WS. The translation insertion should happen in
-    // the WS, not live.
-    $this->switchToWorkspace('stage');
-    $entity_published = $storage->loadUnchanged($entity_published->id());
-    $entity_published->addTranslation('ro', ['name' => 'testAddingTranslationToDefaultRevision']);
-    $entity_published->save();
-
-    $this->assertTrue(\Drupal::state()->get('workspace_was_active'));
-  }
-
 }
-- 
GitLab


From f3a68b28d51042ca5be40609a5f57a34d3b618c5 Mon Sep 17 00:00:00 2001
From: Alec Smrekar <alec@smrekar.me>
Date: Mon, 28 Apr 2025 12:15:00 +0200
Subject: [PATCH 4/5] Code nits and remove unnecessary ifs

---
 .../modules/workspaces/src/Hook/EntityOperations.php | 12 ------------
 .../workspaces_test/src/Hook/WorkspacesTestHooks.php |  2 +-
 2 files changed, 1 insertion(+), 13 deletions(-)

diff --git a/core/modules/workspaces/src/Hook/EntityOperations.php b/core/modules/workspaces/src/Hook/EntityOperations.php
index ce1f316d8d70..80dbdfa546eb 100644
--- a/core/modules/workspaces/src/Hook/EntityOperations.php
+++ b/core/modules/workspaces/src/Hook/EntityOperations.php
@@ -10,10 +10,8 @@
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Entity\Query\QueryInterface;
 use Drupal\Core\Entity\RevisionableInterface;
-use Drupal\Core\Entity\SynchronizableInterface;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Hook\Attribute\Hook;
-use Drupal\Core\TypedData\TranslatableInterface;
 use Drupal\workspaces\WorkspaceAssociationInterface;
 use Drupal\workspaces\WorkspaceInformationInterface;
 use Drupal\workspaces\WorkspaceManagerInterface;
@@ -207,16 +205,6 @@ public function entityTranslationInsert(EntityInterface $translation): void {
         ->getStorage($translation->getEntityTypeId())
         ->load($translation->id());
     });
-
-    if (!$default_revision instanceof TranslatableInterface) {
-      return;
-    }
-    if (!$default_revision instanceof SynchronizableInterface) {
-      return;
-    }
-    if (!$default_revision instanceof EntityPublishedInterface) {
-      return;
-    }
     $langcode = $translation->language()->getId();
     if (!$default_revision->hasTranslation($langcode)) {
       $default_revision_translation = $default_revision->addTranslation($langcode, $translation->toArray());
diff --git a/core/modules/workspaces/tests/modules/workspaces_test/src/Hook/WorkspacesTestHooks.php b/core/modules/workspaces/tests/modules/workspaces_test/src/Hook/WorkspacesTestHooks.php
index 8c9b3cbf629f..49a85036703c 100644
--- a/core/modules/workspaces/tests/modules/workspaces_test/src/Hook/WorkspacesTestHooks.php
+++ b/core/modules/workspaces/tests/modules/workspaces_test/src/Hook/WorkspacesTestHooks.php
@@ -29,8 +29,8 @@ public function entityTypeAlter(array &$entity_types) : void {
    */
   #[Hook('entity_test_mulrevpub_translation_create')]
   public function entityTranslationCreate(): void {
-    $workspace_manager = \Drupal::service('workspaces.manager');
     /** @var \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager */
+    $workspace_manager = \Drupal::service('workspaces.manager');
     \Drupal::state()->set('workspace_was_active', $workspace_manager->hasActiveWorkspace());
   }
 
-- 
GitLab


From 56ac623f1b2c8f420c0f93d78302879999cf9309 Mon Sep 17 00:00:00 2001
From: Alec Smrekar <alec@smrekar.me>
Date: Mon, 5 May 2025 11:10:41 +0200
Subject: [PATCH 5/5] Replace state with KV

---
 .../modules/workspaces_test/src/Hook/WorkspacesTestHooks.php    | 2 +-
 .../tests/src/Kernel/WorkspaceContentTranslationTest.php        | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/core/modules/workspaces/tests/modules/workspaces_test/src/Hook/WorkspacesTestHooks.php b/core/modules/workspaces/tests/modules/workspaces_test/src/Hook/WorkspacesTestHooks.php
index 49a85036703c..d452d343ca3d 100644
--- a/core/modules/workspaces/tests/modules/workspaces_test/src/Hook/WorkspacesTestHooks.php
+++ b/core/modules/workspaces/tests/modules/workspaces_test/src/Hook/WorkspacesTestHooks.php
@@ -31,7 +31,7 @@ public function entityTypeAlter(array &$entity_types) : void {
   public function entityTranslationCreate(): void {
     /** @var \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager */
     $workspace_manager = \Drupal::service('workspaces.manager');
-    \Drupal::state()->set('workspace_was_active', $workspace_manager->hasActiveWorkspace());
+    \Drupal::keyValue('ws_test')->set('workspace_was_active', $workspace_manager->hasActiveWorkspace());
   }
 
 }
diff --git a/core/modules/workspaces/tests/src/Kernel/WorkspaceContentTranslationTest.php b/core/modules/workspaces/tests/src/Kernel/WorkspaceContentTranslationTest.php
index a31ad20d0adb..e3792cfaa325 100644
--- a/core/modules/workspaces/tests/src/Kernel/WorkspaceContentTranslationTest.php
+++ b/core/modules/workspaces/tests/src/Kernel/WorkspaceContentTranslationTest.php
@@ -86,7 +86,7 @@ public function testTranslations(): void {
     $entity_published->save();
 
     // Test that the default revision translation is created in a WS.
-    $this->assertTrue(\Drupal::state()->get('workspace_was_active'));
+    $this->assertTrue(\Drupal::keyValue('ws_test')->get('workspace_was_active'));
 
     $entity_unpublished->addTranslation('ro', ['name' => 'live - 2 - unpublished - RO']);
     $entity_unpublished->save();
-- 
GitLab