diff --git a/core/modules/content_moderation/src/ModerationInformation.php b/core/modules/content_moderation/src/ModerationInformation.php index 9f350467be6ab1d772cdcf764e32522d628ce08a..286104ffd78cfb892e6baef3bfcc8d9ebd718687 100644 --- a/core/modules/content_moderation/src/ModerationInformation.php +++ b/core/modules/content_moderation/src/ModerationInformation.php @@ -175,6 +175,10 @@ public function isLiveRevision(ContentEntityInterface $entity) { public function isDefaultRevisionPublished(ContentEntityInterface $entity) { $workflow = $this->getWorkflowForEntity($entity); $default_revision = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->load($entity->id()); + // If no default revision could be loaded, the entity has not yet been + // saved. In this case the moderation_state of the unsaved entity can be + // used, since once saved it will become the default. + $default_revision = $default_revision ?: $entity; // Ensure we are checking all translations of the default revision. if ($default_revision instanceof TranslatableInterface && $default_revision->isTranslatable()) { diff --git a/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php b/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php index 5580630b86a79bc9cb18aff93afdec0bd5f76ba1..af0bb4fd7d6de24f053d62005141ab6b633dacc2 100644 --- a/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php +++ b/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php @@ -251,4 +251,34 @@ public function entityUnserializeTestCases() { ]; } + /** + * Test saving a moderated node with an existing ID. + * + * @dataProvider moderatedEntityWithExistingIdTestCases + */ + public function testModeratedEntityWithExistingId($state) { + $node = Node::create([ + 'title' => 'Test title', + 'type' => 'example', + 'nid' => 999, + 'moderation_state' => $state, + ]); + $node->save(); + $this->assertEquals($state, $node->moderation_state->value); + } + + /** + * Test cases for ::testModeratedEntityWithExistingId. + */ + public function moderatedEntityWithExistingIdTestCases() { + return [ + 'Draft non-default state' => [ + 'draft', + ], + 'Published default state' => [ + 'published', + ], + ]; + } + }