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

namespace Drupal\node\Form;

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

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

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

  /**
   * The node storage.
   *
31
   * @var \Drupal\node\NodeStorageInterface
32 33 34
   */
  protected $nodeStorage;

35 36 37
  /**
   * The date formatter service.
   *
38
   * @var \Drupal\Core\Datetime\DateFormatterInterface
39 40 41
   */
  protected $dateFormatter;

42 43 44 45 46 47 48
  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

49 50 51
  /**
   * Constructs a new NodeRevisionRevertForm.
   *
52
   * @param \Drupal\Core\Entity\EntityStorageInterface $node_storage
53
   *   The node storage.
54
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
55
   *   The date formatter service.
56 57
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
58
   */
59
  public function __construct(EntityStorageInterface $node_storage, DateFormatterInterface $date_formatter, TimeInterface $time) {
60
    $this->nodeStorage = $node_storage;
61
    $this->dateFormatter = $date_formatter;
62
    $this->time = $time;
63 64 65 66 67 68 69
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
70
      $container->get('entity.manager')->getStorage('node'),
71 72
      $container->get('date.formatter'),
      $container->get('datetime.time')
73 74 75 76 77 78
    );
  }

  /**
   * {@inheritdoc}
   */
79
  public function getFormId() {
80 81 82 83 84 85 86
    return 'node_revision_revert_confirm';
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
87
    return t('Are you sure you want to revert to the revision from %revision-date?', ['%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime())]);
88 89 90 91 92
  }

  /**
   * {@inheritdoc}
   */
93
  public function getCancelUrl() {
94
    return new Url('entity.node.version_history', ['node' => $this->revision->id()]);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  }

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

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

  /**
   * {@inheritdoc}
   */
114
  public function buildForm(array $form, FormStateInterface $form_state, $node_revision = NULL) {
115
    $this->revision = $this->nodeStorage->loadRevision($node_revision);
116 117 118
    $form = parent::buildForm($form, $form_state);

    return $form;
119 120 121 122 123
  }

  /**
   * {@inheritdoc}
   */
124
  public function submitForm(array &$form, FormStateInterface $form_state) {
125 126 127
    // 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();
128

129 130
    $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)]);
131
    $this->revision->setRevisionUserId($this->currentUser()->id());
132 133
    $this->revision->setRevisionCreationTime($this->time->getRequestTime());
    $this->revision->setChangedTime($this->time->getRequestTime());
134
    $this->revision->save();
135

136
    $this->logger('content')->notice('@type: reverted %title revision %revision.', ['@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
137 138 139 140 141 142
    $this->messenger()
      ->addStatus($this->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),
      ]));
143
    $form_state->setRedirect(
144
      'entity.node.version_history',
145
      ['node' => $this->revision->id()]
146
    );
147 148
  }

149 150 151 152 153
  /**
   * Prepares a revision to be reverted.
   *
   * @param \Drupal\node\NodeInterface $revision
   *   The revision to be reverted.
154 155
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
156 157 158 159
   *
   * @return \Drupal\node\NodeInterface
   *   The prepared revision ready to be stored.
   */
160
  protected function prepareRevertedRevision(NodeInterface $revision, FormStateInterface $form_state) {
161 162 163 164 165 166
    $revision->setNewRevision();
    $revision->isDefaultRevision(TRUE);

    return $revision;
  }

167
}