3 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;
10 use Drupal\node\NodeInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Provides a form for reverting a node revision.
16 class NodeRevisionRevertForm extends ConfirmFormBase {
21 * @var \Drupal\node\NodeInterface
28 * @var \Drupal\Core\Entity\EntityStorageInterface
30 protected $nodeStorage;
33 * The date formatter service.
35 * @var \Drupal\Core\Datetime\DateFormatterInterface
37 protected $dateFormatter;
42 * @var \Drupal\Component\Datetime\TimeInterface
47 * Constructs a new NodeRevisionRevertForm.
49 * @param \Drupal\Core\Entity\EntityStorageInterface $node_storage
51 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
52 * The date formatter service.
54 public function __construct(EntityStorageInterface $node_storage, DateFormatterInterface $date_formatter) {
55 $this->nodeStorage = $node_storage;
56 $this->dateFormatter = $date_formatter;
57 $this->time = \Drupal::service('datetime.time');
63 public static function create(ContainerInterface $container) {
65 $container->get('entity.manager')->getStorage('node'),
66 $container->get('date.formatter')
73 public function getFormId() {
74 return 'node_revision_revert_confirm';
80 public function getQuestion() {
81 return t('Are you sure you want to revert to the revision from %revision-date?', ['%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime())]);
87 public function getCancelUrl() {
88 return new Url('entity.node.version_history', ['node' => $this->revision->id()]);
94 public function getConfirmText() {
101 public function getDescription() {
108 public function buildForm(array $form, FormStateInterface $form_state, $node_revision = NULL) {
109 $this->revision = $this->nodeStorage->loadRevision($node_revision);
110 $form = parent::buildForm($form, $form_state);
118 public function submitForm(array &$form, FormStateInterface $form_state) {
119 // The revision timestamp will be updated when the revision is saved. Keep
120 // the original one for the confirmation message.
121 $original_revision_timestamp = $this->revision->getRevisionCreationTime();
123 $this->revision = $this->prepareRevertedRevision($this->revision, $form_state);
124 $this->revision->revision_log = t('Copy of the revision from %date.', ['%date' => $this->dateFormatter->format($original_revision_timestamp)]);
125 $this->revision->setRevisionCreationTime($this->time->getRequestTime());
126 $this->revision->setChangedTime($this->time->getRequestTime());
127 $this->revision->save();
129 $this->logger('content')->notice('@type: reverted %title revision %revision.', ['@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
130 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)]));
131 $form_state->setRedirect(
132 'entity.node.version_history',
133 ['node' => $this->revision->id()]
138 * Prepares a revision to be reverted.
140 * @param \Drupal\node\NodeInterface $revision
141 * The revision to be reverted.
142 * @param \Drupal\Core\Form\FormStateInterface $form_state
143 * The current state of the form.
145 * @return \Drupal\node\NodeInterface
146 * The prepared revision ready to be stored.
148 protected function prepareRevertedRevision(NodeInterface $revision, FormStateInterface $form_state) {
149 $revision->setNewRevision();
150 $revision->isDefaultRevision(TRUE);