Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / node / src / Form / NodeRevisionRevertForm.php
1 <?php
2
3 namespace Drupal\node\Form;
4
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 use Drupal\node\NodeInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Provides a form for reverting a node revision.
16  */
17 class NodeRevisionRevertForm extends ConfirmFormBase {
18
19   /**
20    * The node revision.
21    *
22    * @var \Drupal\node\NodeInterface
23    */
24   protected $revision;
25
26   /**
27    * The node storage.
28    *
29    * @var \Drupal\Core\Entity\EntityStorageInterface
30    */
31   protected $nodeStorage;
32
33   /**
34    * The date formatter service.
35    *
36    * @var \Drupal\Core\Datetime\DateFormatterInterface
37    */
38   protected $dateFormatter;
39
40   /**
41    * The time service.
42    *
43    * @var \Drupal\Component\Datetime\TimeInterface
44    */
45   protected $time;
46
47   /**
48    * Constructs a new NodeRevisionRevertForm.
49    *
50    * @param \Drupal\Core\Entity\EntityStorageInterface $node_storage
51    *   The node storage.
52    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
53    *   The date formatter service.
54    * @param \Drupal\Component\Datetime\TimeInterface $time
55    *   The time service.
56    */
57   public function __construct(EntityStorageInterface $node_storage, DateFormatterInterface $date_formatter, TimeInterface $time) {
58     $this->nodeStorage = $node_storage;
59     $this->dateFormatter = $date_formatter;
60     $this->time = $time;
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public static function create(ContainerInterface $container) {
67     return new static(
68       $container->get('entity.manager')->getStorage('node'),
69       $container->get('date.formatter'),
70       $container->get('datetime.time')
71     );
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function getFormId() {
78     return 'node_revision_revert_confirm';
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function getQuestion() {
85     return t('Are you sure you want to revert to the revision from %revision-date?', ['%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime())]);
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function getCancelUrl() {
92     return new Url('entity.node.version_history', ['node' => $this->revision->id()]);
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function getConfirmText() {
99     return t('Revert');
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public function getDescription() {
106     return '';
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   public function buildForm(array $form, FormStateInterface $form_state, $node_revision = NULL) {
113     $this->revision = $this->nodeStorage->loadRevision($node_revision);
114     $form = parent::buildForm($form, $form_state);
115
116     return $form;
117   }
118
119   /**
120    * {@inheritdoc}
121    */
122   public function submitForm(array &$form, FormStateInterface $form_state) {
123     // The revision timestamp will be updated when the revision is saved. Keep
124     // the original one for the confirmation message.
125     $original_revision_timestamp = $this->revision->getRevisionCreationTime();
126
127     $this->revision = $this->prepareRevertedRevision($this->revision, $form_state);
128     $this->revision->revision_log = t('Copy of the revision from %date.', ['%date' => $this->dateFormatter->format($original_revision_timestamp)]);
129     $this->revision->setRevisionCreationTime($this->time->getRequestTime());
130     $this->revision->setChangedTime($this->time->getRequestTime());
131     $this->revision->save();
132
133     $this->logger('content')->notice('@type: reverted %title revision %revision.', ['@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
134     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)]));
135     $form_state->setRedirect(
136       'entity.node.version_history',
137       ['node' => $this->revision->id()]
138     );
139   }
140
141   /**
142    * Prepares a revision to be reverted.
143    *
144    * @param \Drupal\node\NodeInterface $revision
145    *   The revision to be reverted.
146    * @param \Drupal\Core\Form\FormStateInterface $form_state
147    *   The current state of the form.
148    *
149    * @return \Drupal\node\NodeInterface
150    *   The prepared revision ready to be stored.
151    */
152   protected function prepareRevertedRevision(NodeInterface $revision, FormStateInterface $form_state) {
153     $revision->setNewRevision();
154     $revision->isDefaultRevision(TRUE);
155
156     return $revision;
157   }
158
159 }