diff --git a/core/modules/workspaces/src/Hook/EntityOperations.php b/core/modules/workspaces/src/Hook/EntityOperations.php index c459f4d065e3fbd47998ffa6941a561452993034..80dbdfa546ebc6cc1dc99c5c0071c8bd15d5ebe7 100644 --- a/core/modules/workspaces/src/Hook/EntityOperations.php +++ b/core/modules/workspaces/src/Hook/EntityOperations.php @@ -200,18 +200,19 @@ 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()); }); + $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(); + } } /** 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 09ab54054ff6e5b97888ece8944079cb335fc944..d452d343ca3d1f316bfe44976bef73a5854bcf88 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 { + /** @var \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager */ + $workspace_manager = \Drupal::service('workspaces.manager'); + \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 70351c96318716f9311923cc362b515336775ad0..e3792cfaa325b931d0d451ecda03b0f7d3259aa1 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', ]; /** @@ -84,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::keyValue('ws_test')->get('workspace_was_active')); + $entity_unpublished->addTranslation('ro', ['name' => 'live - 2 - unpublished - RO']); $entity_unpublished->save();