Unverified Commit 884786d6 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3065802 by Sam152, mtodor, alexpott: Preview and content moderation...

Issue #3065802 by Sam152, mtodor, alexpott: Preview and content moderation widget broken for unsaved entities
parent 78cb3fc6
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -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);
+17 −4
Original line number Diff line number Diff line
@@ -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());