3 namespace Drupal\node\Form;
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;
11 use Drupal\node\NodeInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Provides a form for reverting a node revision.
19 class NodeRevisionRevertForm extends ConfirmFormBase {
24 * @var \Drupal\node\NodeInterface
31 * @var \Drupal\node\NodeStorageInterface
33 protected $nodeStorage;
36 * The date formatter service.
38 * @var \Drupal\Core\Datetime\DateFormatterInterface
40 protected $dateFormatter;
45 * @var \Drupal\Component\Datetime\TimeInterface
50 * Constructs a new NodeRevisionRevertForm.
52 * @param \Drupal\Core\Entity\EntityStorageInterface $node_storage
54 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
55 * The date formatter service.
56 * @param \Drupal\Component\Datetime\TimeInterface $time
59 public function __construct(EntityStorageInterface $node_storage, DateFormatterInterface $date_formatter, TimeInterface $time) {
60 $this->nodeStorage = $node_storage;
61 $this->dateFormatter = $date_formatter;
68 public static function create(ContainerInterface $container) {
70 $container->get('entity.manager')->getStorage('node'),
71 $container->get('date.formatter'),
72 $container->get('datetime.time')
79 public function getFormId() {
80 return 'node_revision_revert_confirm';
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())]);
93 public function getCancelUrl() {
94 return new Url('entity.node.version_history', ['node' => $this->revision->id()]);
100 public function getConfirmText() {
107 public function getDescription() {
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);
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();
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();
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()]
144 * Prepares a revision to be reverted.
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.
151 * @return \Drupal\node\NodeInterface
152 * The prepared revision ready to be stored.
154 protected function prepareRevertedRevision(NodeInterface $revision, FormStateInterface $form_state) {
155 $revision->setNewRevision();
156 $revision->isDefaultRevision(TRUE);