diff --git a/core/modules/workspaces/src/Entity/Workspace.php b/core/modules/workspaces/src/Entity/Workspace.php index fdde2399a712a0200d071f4e47fdf71e3099589c..62a83226561602e689aff95a7cb205a2efc6a5ac 100644 --- a/core/modules/workspaces/src/Entity/Workspace.php +++ b/core/modules/workspaces/src/Entity/Workspace.php @@ -174,16 +174,27 @@ public static function preDelete(EntityStorageInterface $storage, array $entitie public static function postDelete(EntityStorageInterface $storage, array $entities) { parent::postDelete($storage, $entities); - // Add the IDs of the deleted workspaces to the list of workspaces that will - // be purged on cron. - $state = \Drupal::state(); - $deleted_workspace_ids = $state->get('workspace.deleted', []); - $deleted_workspace_ids += array_combine(array_keys($entities), array_keys($entities)); - $state->set('workspace.deleted', $deleted_workspace_ids); - - // Trigger a batch purge to allow empty workspaces to be deleted - // immediately. - \Drupal::service('workspaces.manager')->purgeDeletedWorkspacesBatch(); + /** @var \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager */ + $workspace_manager = \Drupal::service('workspaces.manager'); + // Disable the currently active workspace if it has been deleted. + if ($workspace_manager->hasActiveWorkspace() + && in_array($workspace_manager->getActiveWorkspace()->id(), array_keys($entities), TRUE)) { + $workspace_manager->switchToLive(); + } + + // Ensure that workspace batch purging does not happen inside a workspace. + $workspace_manager->executeOutsideWorkspace(function () use ($workspace_manager, $entities) { + // Add the IDs of the deleted workspaces to the list of workspaces that will + // be purged on cron. + $state = \Drupal::state(); + $deleted_workspace_ids = $state->get('workspace.deleted', []); + $deleted_workspace_ids += array_combine(array_keys($entities), array_keys($entities)); + $state->set('workspace.deleted', $deleted_workspace_ids); + + // Trigger a batch purge to allow empty workspaces to be deleted + // immediately. + $workspace_manager->purgeDeletedWorkspacesBatch(); + }); } } diff --git a/core/modules/workspaces/tests/src/Kernel/WorkspaceCRUDTest.php b/core/modules/workspaces/tests/src/Kernel/WorkspaceCRUDTest.php index 84319ab5342d5c0c6c3f4461abf6a34f18456238..4981c0d9506810096f89de3ef6525a9c6a8b729a 100644 --- a/core/modules/workspaces/tests/src/Kernel/WorkspaceCRUDTest.php +++ b/core/modules/workspaces/tests/src/Kernel/WorkspaceCRUDTest.php @@ -201,6 +201,9 @@ public function testDeletingWorkspaces() { $workspace_deleted = \Drupal::state()->get('workspace.deleted'); $this->assertCount(0, $workspace_deleted); + + // Check that the deleted workspace is no longer active. + $this->assertFalse($this->workspaceManager->hasActiveWorkspace()); } /**