diff --git a/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php b/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php index a571fed7c2d49e19d0fd2cfa37fda12ae45e7360..8bfba8a35cc62d3c1ad91340ea109982c66998ce 100644 --- a/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php +++ b/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php @@ -122,10 +122,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $default = $this->moderationInformation->getOriginalState($entity); - // If the entity is not new, grab the most recent revision and - // load it. The moderation state of the saved revision will be used - // to display the current state as well determine the the appropriate - // transitions. + // If the entity already exists, grab the most recent revision and load it. + // The moderation state of the saved revision will be used to display the + // current state as well determine the the appropriate transitions. if (!$entity->isNew()) { /** @var \Drupal\Core\Entity\ContentEntityInterface $original_entity */ $original_entity = $this->entityTypeManager->getStorage($entity->getEntityTypeId())->loadRevision($entity->getLoadedRevisionId()); @@ -133,6 +132,13 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $original_entity = $original_entity->getTranslation($entity->language()->getId()); } } + // For a new entity, ensure the moderation state of the original entity is + // always the default state. Despite the entity being unsaved, it may have + // previously been set to a new target state, for example previewed entities + // are retrieved from temporary storage with field values set. + else { + $original_entity->set('moderation_state', $default->id()); + } /** @var \Drupal\workflows\Transition[] $transitions */ $transitions = $this->validator->getValidTransitions($original_entity, $this->currentUser); diff --git a/core/modules/content_moderation/tests/src/Functional/ModerationFormTest.php b/core/modules/content_moderation/tests/src/Functional/ModerationFormTest.php index b631ffcfd06e0f175d26147ba25168c1fc9ed693..679885c05900de254c7717629e05431ea7fcfe8a 100644 --- a/core/modules/content_moderation/tests/src/Functional/ModerationFormTest.php +++ b/core/modules/content_moderation/tests/src/Functional/ModerationFormTest.php @@ -43,12 +43,25 @@ protected function setUp() { * @see \Drupal\Tests\content_moderation\Functional\ModerationStateBlockTest::testCustomBlockModeration */ public function testModerationForm() { - // Create new moderated content in draft. - $this->drupalPostForm('node/add/moderated_content', [ + // Test the states that appear by default when creating a new item of + // content. + $this->drupalGet('node/add/moderated_content'); + $this->assertSession()->optionExists('moderation_state[0][state]', 'draft'); + $this->assertSession()->optionExists('moderation_state[0][state]', 'published'); + $this->assertSession()->optionNotExists('moderation_state[0][state]', 'archived'); + // Previewing a new item of content should not change the available states. + $this->submitForm([ + 'moderation_state[0][state]' => 'published', 'title[0][value]' => 'Some moderated content', 'body[0][value]' => 'First version of the content.', - 'moderation_state[0][state]' => 'draft', - ], t('Save')); + ], 'Preview'); + $this->clickLink('Back to content editing'); + $this->assertSession()->optionExists('moderation_state[0][state]', 'draft'); + $this->assertSession()->optionExists('moderation_state[0][state]', 'published'); + $this->assertSession()->optionNotExists('moderation_state[0][state]', 'archived'); + + // Create new moderated content in draft. + $this->submitForm(['moderation_state[0][state]' => 'draft'], t('Save')); $node = $this->drupalGetNodeByTitle('Some moderated content'); $canonical_path = sprintf('node/%d', $node->id());