Version 1
[yaffs-website] / web / core / modules / node / src / Form / NodeRevisionRevertForm.php
1 <?php
2
3 namespace Drupal\node\Form;
4
5 use Drupal\Core\Datetime\DateFormatterInterface;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Form\ConfirmFormBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Url;
10 use Drupal\node\NodeInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Provides a form for reverting a node revision.
15  */
16 class NodeRevisionRevertForm extends ConfirmFormBase {
17
18   /**
19    * The node revision.
20    *
21    * @var \Drupal\node\NodeInterface
22    */
23   protected $revision;
24
25   /**
26    * The node storage.
27    *
28    * @var \Drupal\Core\Entity\EntityStorageInterface
29    */
30   protected $nodeStorage;
31
32   /**
33    * The date formatter service.
34    *
35    * @var \Drupal\Core\Datetime\DateFormatterInterface
36    */
37   protected $dateFormatter;
38
39   /**
40    * The time service.
41    *
42    * @var \Drupal\Component\Datetime\TimeInterface
43    */
44   protected $time;
45
46   /**
47    * Constructs a new NodeRevisionRevertForm.
48    *
49    * @param \Drupal\Core\Entity\EntityStorageInterface $node_storage
50    *   The node storage.
51    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
52    *   The date formatter service.
53    */
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');
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public static function create(ContainerInterface $container) {
64     return new static(
65       $container->get('entity.manager')->getStorage('node'),
66       $container->get('date.formatter')
67     );
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function getFormId() {
74     return 'node_revision_revert_confirm';
75   }
76
77   /**
78    * {@inheritdoc}
79    */
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())]);
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function getCancelUrl() {
88     return new Url('entity.node.version_history', ['node' => $this->revision->id()]);
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function getConfirmText() {
95     return t('Revert');
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function getDescription() {
102     return '';
103   }
104
105   /**
106    * {@inheritdoc}
107    */
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);
111
112     return $form;
113   }
114
115   /**
116    * {@inheritdoc}
117    */
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();
122
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();
128
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()]
134     );
135   }
136
137   /**
138    * Prepares a revision to be reverted.
139    *
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.
144    *
145    * @return \Drupal\node\NodeInterface
146    *   The prepared revision ready to be stored.
147    */
148   protected function prepareRevertedRevision(NodeInterface $revision, FormStateInterface $form_state) {
149     $revision->setNewRevision();
150     $revision->isDefaultRevision(TRUE);
151
152     return $revision;
153   }
154
155 }