diff --git a/core/modules/node/src/Controller/NodeController.php b/core/modules/node/src/Controller/NodeController.php index f4a30ee..3cd8b26 100644 --- a/core/modules/node/src/Controller/NodeController.php +++ b/core/modules/node/src/Controller/NodeController.php @@ -196,8 +196,10 @@ public function revisionOverview(NodeInterface $node) { . (($revision->revision_log->value != '') ? '

' . Xss::filter($revision->revision_log->value) . '

' : ''); if ($revert_permission) { + $future_text = $node->isPublished() ? $this->t('Publish') : $this->t('Set current'); + $button_text = $node->getRevisionId() < $vid ? $future_text : $this->t('Revert'); $links['revert'] = array( - 'title' => $this->t('Revert'), + 'title' => $button_text, 'url' => Url::fromRoute('node.revision_revert_confirm', ['node' => $node->id(), 'node_revision' => $vid]), ); } diff --git a/core/modules/node/src/Form/NodeRevisionRevertForm.php b/core/modules/node/src/Form/NodeRevisionRevertForm.php index d6e748e..795e0a9 100644 --- a/core/modules/node/src/Form/NodeRevisionRevertForm.php +++ b/core/modules/node/src/Form/NodeRevisionRevertForm.php @@ -11,6 +11,7 @@ use Drupal\Core\Form\ConfirmFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; +use Drupal\node\Entity\Node; use Drupal\node\NodeInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -63,7 +64,18 @@ public function getFormId() { * {@inheritdoc} */ public function getQuestion() { - return t('Are you sure you want to revert to the revision from %revision-date?', array('%revision-date' => format_date($this->revision->getRevisionCreationTime()))); + $args = array('%revision-date' => format_date($this->revision->getRevisionCreationTime())); + if ($this->versionIsNewer() && $this->getCurrentNode()->isPublished()) { + // If the version is newer, and the node will be published, use 'publish' + // in the confirmation text. + return $this->t('Are you sure you want to publish the version from %revision-date?', $args); + } + elseif ($this->versionIsNewer()) { + // If the version is newer than the current version, change to 'set current'. + return $this->t('Are you sure you want to set the current version to %revision-date?', $args); + } + + return $this->t('Are you sure you want to revert to the revision from %revision-date?', $args); } /** @@ -77,7 +89,7 @@ public function getCancelUrl() { * {@inheritdoc} */ public function getConfirmText() { - return t('Revert'); + return $this->versionIsNewer() ? $this->t('Set current version') : $this->t('Revert'); } /** @@ -121,4 +133,20 @@ public function submitForm(array &$form, FormStateInterface $form_state) { ); } + /** + * Helper function to determine if this version is newer than current. + */ + protected function versionIsNewer() { + return $this->revision->getRevisionId() > $this->getCurrentNode()->getRevisionId(); + } + + /** + * Get current node. + * + * @return \Drupal\node\NodeInterface + */ + protected function getCurrentNode() { + return $this->nodeStorage->load($this->revision->id()); + } + } diff --git a/core/modules/node/src/Tests/NodeRevisionsUiTest.php b/core/modules/node/src/Tests/NodeRevisionsUiTest.php index 2438b60..5c219ca 100644 --- a/core/modules/node/src/Tests/NodeRevisionsUiTest.php +++ b/core/modules/node/src/Tests/NodeRevisionsUiTest.php @@ -6,6 +6,7 @@ */ namespace Drupal\node\Tests; +use Drupal\Component\Utility\String; /** * Tests the UI for controlling node revision behavior. @@ -72,5 +73,51 @@ function testNodeFormSaveWithoutRevision() { $node_revision = $node_storage->load($node->id()); $this->assertNotEqual($node_revision->getRevisionId(), $node->getRevisionId(), "After an existing node is saved with 'Create new revision' checked, a new revision is created."); + // Create a new revision that isn't 'front facing' to verify UI on overview. + $newest_revision = clone ($node); + $newest_revision->isDefaultRevision(FALSE); + $newest_revision->setNewRevision(); + $newest_revision->save(); + $this->drupalGet('node/' . $node->id() . '/revisions'); + $this->assertRevisionButtonOrder([t('Publish'), t('current revision'), t('Revert')]); + + // Unpublish the node and the row text should change to 'set current'. + $node_revision->setPublished(FALSE); + $node_revision->setNewRevision(FALSE); + $node_revision->save(); + $this->drupalGet('node/' . $node->id() . '/revisions'); + $this->assertRevisionButtonOrder([t('Set current'), t('current revision'), t('Revert')]); + + // Set the most recent revision to current. + $this->clickLink(t('Set current')); + $this->drupalPostForm('node/' . $node->id() . '/revisions/' . $newest_revision->getRevisionId() . '/revert', [], t('Set current version')); + $this->assertRevisionButtonOrder([t('current revision'), t('Revert'), t('Revert'), t('Revert')]); } + + /** + * Helper method to assert order of buttons on node revision overview. + * + * @param array $buttons + * Array of button text to look for on the node revision overview page. + * @param string $message + * Success message to print. + */ + protected function assertRevisionButtonOrder(array $buttons, $message = 'Proper revision operations button order found.') { + // Load all buttons in the 'Operations' column. + $found_buttons = $this->xpath('//table/tbody/tr/td[count(//table/thead/tr/th[.=:operations]/preceding-sibling::th)+1]', [':operations' => t('Operations')]); + + if (count($found_buttons) != count($buttons)) { + return $this->fail($message); + } + + foreach ($buttons as $key => $text) { + $found = $found_buttons[$key]->asXML(); + if ($this->assertTrue(strpos($found, $text) !== FALSE, String::format('Button/text !button found.', ['!button' => $text]))) { + unset($buttons[$key]); + } + } + + $this->assertTrue(empty($buttons), $message); + } + }