Updated to Drupal 8.5. Core Media not yet in use.
[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->setRevisionCreationTime($this->time->getRequestTime());
132     $this->revision->setChangedTime($this->time->getRequestTime());
133     $this->revision->save();
134
135     $this->logger('content')->notice('@type: reverted %title revision %revision.', ['@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
136     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)]));
137     $form_state->setRedirect(
138       'entity.node.version_history',
139       ['node' => $this->revision->id()]
140     );
141   }
142
143   /**
144    * Prepares a revision to be reverted.
145    *
146    * @param \Drupal\node\NodeInterface $revision
147    *   The revision to be reverted.
148    * @param \Drupal\Core\Form\FormStateInterface $form_state
149    *   The current state of the form.
150    *
151    * @return \Drupal\node\NodeInterface
152    *   The prepared revision ready to be stored.
153    */
154   protected function prepareRevertedRevision(NodeInterface $revision, FormStateInterface $form_state) {
155     $revision->setNewRevision();
156     $revision->isDefaultRevision(TRUE);
157
158     return $revision;
159   }
160
161 }