From 2cfb07381acf78636d223923075d547455f43c6f Mon Sep 17 00:00:00 2001
From: catch <6915-catch@users.noreply.drupalcode.org>
Date: Tue, 15 Oct 2024 15:03:41 +0100
Subject: [PATCH] Issue #3438769 by vinmayiswamy, amateescu, catch, antonnavi,
 michelle: Sub workspace does not clear

---
 core/modules/workspaces/src/WorkspaceAssociation.php      | 7 ++++++-
 .../tests/src/Kernel/WorkspaceAssociationTest.php         | 8 ++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/core/modules/workspaces/src/WorkspaceAssociation.php b/core/modules/workspaces/src/WorkspaceAssociation.php
index 02d6924c4e4b..8a93a83e34c5 100644
--- a/core/modules/workspaces/src/WorkspaceAssociation.php
+++ b/core/modules/workspaces/src/WorkspaceAssociation.php
@@ -391,7 +391,12 @@ public static function getSubscribedEvents(): array {
    *   The workspace publish event.
    */
   public function onPostPublish(WorkspacePublishEvent $event): void {
-    $this->deleteAssociations($event->getWorkspace()->id());
+    // Cleanup associations for the published workspace as well as its
+    // descendants.
+    $affected_workspaces = $this->workspaceRepository->getDescendantsAndSelf($event->getWorkspace()->id());
+    foreach ($affected_workspaces as $workspace_id) {
+      $this->deleteAssociations($workspace_id);
+    }
   }
 
 }
diff --git a/core/modules/workspaces/tests/src/Kernel/WorkspaceAssociationTest.php b/core/modules/workspaces/tests/src/Kernel/WorkspaceAssociationTest.php
index 4d7e8d80e628..c641c5dcd570 100644
--- a/core/modules/workspaces/tests/src/Kernel/WorkspaceAssociationTest.php
+++ b/core/modules/workspaces/tests/src/Kernel/WorkspaceAssociationTest.php
@@ -156,6 +156,14 @@ public function testWorkspaceAssociation(): void {
     $expected_initial_revisions['dev'] = [8];
 
     $this->assertWorkspaceAssociations('node', $expected_latest_revisions, $expected_all_revisions, $expected_initial_revisions);
+
+    // Publish 'stage' and check the workspace associations.
+    /** @var \Drupal\workspaces\WorkspacePublisherInterface $workspace_publisher */
+    $workspace_publisher = \Drupal::service('workspaces.operation_factory')->getPublisher($this->workspaces['stage']);
+    $workspace_publisher->publish();
+
+    $expected_revisions['stage'] = $expected_revisions['dev'] = [];
+    $this->assertWorkspaceAssociations('node', $expected_revisions, $expected_revisions, $expected_revisions);
   }
 
   /**
-- 
GitLab