diff --git a/src/EntityTypeInfo.php b/src/EntityTypeInfo.php index 9ae41f7..cb7c752 100644 --- a/src/EntityTypeInfo.php +++ b/src/EntityTypeInfo.php @@ -219,6 +219,28 @@ class EntityTypeInfo { $entity = $form_state->getFormObject()->getEntity(); $this->entityTypeManager->getHandler($entity->getEntityTypeId(), 'moderation')->enforceRevisionsEntityFormAlter($form, $form_state, $form_id); + + // Submit handler to redirect to the + $form['actions']['submit']['#submit'][] = [$this, 'bundleFormRedirect']; + } + } + + /** + * Redirect content entity edit forms on save, if there is a forward revision. + * + * When saving their changes, editors should see those changes displayed on + * the next page. + * + * @param array $form + * @param \Drupal\Core\Form\FormStateInterface $form_state + */ + public function bundleFormRedirect(array &$form, FormStateInterface $form_state) { + /* @var ContentEntityInterface $entity */ + $entity = $form_state->getFormObject()->getEntity(); + + if ($this->moderationInfo->hasForwardRevision($entity)) { + $entity_type_id = $entity->getEntityTypeId(); + $form_state->setRedirect("entity.$entity_type_id.latest_version", [$entity_type_id => $entity->id()]); } } } diff --git a/src/Tests/ModerationStateNodeTest.php b/src/Tests/ModerationStateNodeTest.php index ca82e49..1e3223d 100644 --- a/src/Tests/ModerationStateNodeTest.php +++ b/src/Tests/ModerationStateNodeTest.php @@ -7,6 +7,8 @@ namespace Drupal\workbench_moderation\Tests; +use Drupal\Core\Url; + /** * Tests general content moderation workflow for nodes.. * @@ -53,4 +55,47 @@ class ModerationStateNodeTest extends ModerationStateTestBase { $this->assertTrue($node->isPublished()); } + /** + * Tests edit form destinations. + */ + public function testFormSaveDestination() { + // Create new moderated content in draft. + $this->drupalPostForm('node/add/moderated_content', [ + 'title[0][value]' => 'Some moderated content', + 'body[0][value]' => 'First version of the content.', + ], t('Save as Draft')); + + $node = $this->drupalGetNodeByTitle('Some moderated content'); + $edit_path = sprintf('node/%d/edit', $node->id()); + + // After saving, we should be at the canonical URL and viewing the first + // revision. + $this->assertUrl(Url::fromRoute('entity.node.canonical', ['node' => $node->id()])); + $this->assertText('First version of the content.'); + + // Make a new forward revision; after saving, we should be on the "Latest + // version" tab. + $this->drupalPostForm($edit_path, [ + 'body[0][value]' => 'Second version of the content.', + ], t('Save and transition to Needs Review')); + $this->assertUrl(Url::fromRoute('entity.node.latest_version', ['node' => $node->id()])); + $this->assertText('Second version of the content.'); + + // Make a new published revision; after saving, we should be at the + // canonical URL. + $this->drupalPostForm($edit_path, [ + 'body[0][value]' => 'Third version of the content.', + ], t('Save and transition to Published')); + $this->assertUrl(Url::fromRoute('entity.node.canonical', ['node' => $node->id()])); + $this->assertText('Third version of the content.'); + + // Make a new forward revision; after saving, we should once again be on the + // "Latest version" tab. + $this->drupalPostForm($edit_path, [ + 'body[0][value]' => 'Fourth version of the content.', + ], t('Save and create new revision in Draft')); + $this->assertUrl(Url::fromRoute('entity.node.latest_version', ['node' => $node->id()])); + $this->assertText('Fourth version of the content.'); + } + } diff --git a/src/Tests/ModerationStateTestBase.php b/src/Tests/ModerationStateTestBase.php index 9da7743..a4a3ef6 100644 --- a/src/Tests/ModerationStateTestBase.php +++ b/src/Tests/ModerationStateTestBase.php @@ -41,6 +41,7 @@ abstract class ModerationStateTestBase extends WebTestBase { 'access administration pages', 'administer content types', 'administer nodes', + 'view own unpublished content', ]; /**