NodeRevisionRevertForm.php 4.1 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\node\Form;

5
use Drupal\Core\Datetime\DateFormatterInterface;
6
use Drupal\Core\Entity\EntityStorageInterface;
7
use Drupal\Core\Form\ConfirmFormBase;
8
use Drupal\Core\Form\FormStateInterface;
9
use Drupal\Core\Url;
10 11 12 13 14 15
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a form for reverting a node revision.
 */
16
class NodeRevisionRevertForm extends ConfirmFormBase {
17 18 19 20 21 22 23 24 25 26 27

  /**
   * The node revision.
   *
   * @var \Drupal\node\NodeInterface
   */
  protected $revision;

  /**
   * The node storage.
   *
28
   * @var \Drupal\Core\Entity\EntityStorageInterface
29 30 31
   */
  protected $nodeStorage;

32 33 34
  /**
   * The date formatter service.
   *
35
   * @var \Drupal\Core\Datetime\DateFormatterInterface
36 37 38
   */
  protected $dateFormatter;

39 40 41
  /**
   * Constructs a new NodeRevisionRevertForm.
   *
42
   * @param \Drupal\Core\Entity\EntityStorageInterface $node_storage
43
   *   The node storage.
44
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
45
   *   The date formatter service.
46
   */
47
  public function __construct(EntityStorageInterface $node_storage, DateFormatterInterface $date_formatter) {
48
    $this->nodeStorage = $node_storage;
49
    $this->dateFormatter = $date_formatter;
50 51 52 53 54 55 56
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
57 58
      $container->get('entity.manager')->getStorage('node'),
      $container->get('date.formatter')
59 60 61 62 63 64
    );
  }

  /**
   * {@inheritdoc}
   */
65
  public function getFormId() {
66 67 68 69 70 71 72
    return 'node_revision_revert_confirm';
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
73
    return t('Are you sure you want to revert to the revision from %revision-date?', ['%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime())]);
74 75 76 77 78
  }

  /**
   * {@inheritdoc}
   */
79
  public function getCancelUrl() {
80
    return new Url('entity.node.version_history', ['node' => $this->revision->id()]);
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return t('Revert');
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return '';
  }

  /**
   * {@inheritdoc}
   */
100
  public function buildForm(array $form, FormStateInterface $form_state, $node_revision = NULL) {
101
    $this->revision = $this->nodeStorage->loadRevision($node_revision);
102 103 104
    $form = parent::buildForm($form, $form_state);

    return $form;
105 106 107 108 109
  }

  /**
   * {@inheritdoc}
   */
110
  public function submitForm(array &$form, FormStateInterface $form_state) {
111 112 113
    // The revision timestamp will be updated when the revision is saved. Keep
    // the original one for the confirmation message.
    $original_revision_timestamp = $this->revision->getRevisionCreationTime();
114

115 116 117
    $this->revision = $this->prepareRevertedRevision($this->revision, $form_state);
    $this->revision->revision_log = t('Copy of the revision from %date.', ['%date' => $this->dateFormatter->format($original_revision_timestamp)]);
    $this->revision->save();
118

119 120
    $this->logger('content')->notice('@type: reverted %title revision %revision.', ['@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
    drupal_set_message(t('@type %title has been reverted to the revision from %revision-date.', ['@type' => node_get_type_label($this->revision), '%title' => $this->revision->label(), '%revision-date' => $this->dateFormatter->format($original_revision_timestamp)]));
121
    $form_state->setRedirect(
122
      'entity.node.version_history',
123
      ['node' => $this->revision->id()]
124
    );
125 126
  }

127 128 129 130 131
  /**
   * Prepares a revision to be reverted.
   *
   * @param \Drupal\node\NodeInterface $revision
   *   The revision to be reverted.
132 133
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
134 135 136 137
   *
   * @return \Drupal\node\NodeInterface
   *   The prepared revision ready to be stored.
   */
138
  protected function prepareRevertedRevision(NodeInterface $revision, FormStateInterface $form_state) {
139 140 141 142 143 144
    $revision->setNewRevision();
    $revision->isDefaultRevision(TRUE);

    return $revision;
  }

145
}