Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[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  * @internal
18  */
19 class NodeRevisionRevertForm extends ConfirmFormBase {
20
21   /**
22    * The node revision.
23    *
24    * @var \Drupal\node\NodeInterface
25    */
26   protected $revision;
27
28   /**
29    * The node storage.
30    *
31    * @var \Drupal\node\NodeStorageInterface
32    */
33   protected $nodeStorage;
34
35   /**
36    * The date formatter service.
37    *
38    * @var \Drupal\Core\Datetime\DateFormatterInterface
39    */
40   protected $dateFormatter;
41
42   /**
43    * The time service.
44    *
45    * @var \Drupal\Component\Datetime\TimeInterface
46    */
47   protected $time;
48
49   /**
50    * Constructs a new NodeRevisionRevertForm.
51    *
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    * @param \Drupal\Component\Datetime\TimeInterface $time
57    *   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    * {@inheritdoc}
67    */
68   public static function create(ContainerInterface $container) {
69     return new static(
70       $container->get('entity.manager')->getStorage('node'),
71       $container->get('date.formatter'),
72       $container->get('datetime.time')
73     );
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function getFormId() {
80     return 'node_revision_revert_confirm';
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   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    * {@inheritdoc}
92    */
93   public function getCancelUrl() {
94     return new Url('entity.node.version_history', ['node' => $this->revision->id()]);
95   }
96
97   /**
98    * {@inheritdoc}
99    */
100   public function getConfirmText() {
101     return t('Revert');
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function getDescription() {
108     return '';
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public function buildForm(array $form, FormStateInterface $form_state, $node_revision = NULL) {
115     $this->revision = $this->nodeStorage->loadRevision($node_revision);
116     $form = parent::buildForm($form, $form_state);
117
118     return $form;
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   public function submitForm(array &$form, FormStateInterface $form_state) {
125     // The revision timestamp will be updated when the revision is saved. Keep
126     // the original one for the confirmation message.
127     $original_revision_timestamp = $this->revision->getRevisionCreationTime();
128
129     $this->revision = $this->prepareRevertedRevision($this->revision, $form_state);
130     $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     $this->revision->setRevisionCreationTime($this->time->getRequestTime());
133     $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     $this->messenger()
138       ->addStatus($this->t('@type %title has been reverted to the revision from %revision-date.', [
139         '@type' => node_get_type_label($this->revision),
140         '%title' => $this->revision->label(),
141         '%revision-date' => $this->dateFormatter->format($original_revision_timestamp),
142       ]));
143     $form_state->setRedirect(
144       'entity.node.version_history',
145       ['node' => $this->revision->id()]
146     );
147   }
148
149   /**
150    * Prepares a revision to be reverted.
151    *
152    * @param \Drupal\node\NodeInterface $revision
153    *   The revision to be reverted.
154    * @param \Drupal\Core\Form\FormStateInterface $form_state
155    *   The current state of the form.
156    *
157    * @return \Drupal\node\NodeInterface
158    *   The prepared revision ready to be stored.
159    */
160   protected function prepareRevertedRevision(NodeInterface $revision, FormStateInterface $form_state) {
161     $revision->setNewRevision();
162     $revision->isDefaultRevision(TRUE);
163
164     return $revision;
165   }
166
167 }