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.
17 class NodeRevisionRevertForm extends ConfirmFormBase {
22 * @var \Drupal\node\NodeInterface
29 * @var \Drupal\Core\Entity\EntityStorageInterface
31 protected $nodeStorage;
34 * The date formatter service.
36 * @var \Drupal\Core\Datetime\DateFormatterInterface
38 protected $dateFormatter;
43 * @var \Drupal\Component\Datetime\TimeInterface
48 * Constructs a new NodeRevisionRevertForm.
50 * @param \Drupal\Core\Entity\EntityStorageInterface $node_storage
52 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
53 * The date formatter service.
54 * @param \Drupal\Component\Datetime\TimeInterface $time
57 public function __construct(EntityStorageInterface $node_storage, DateFormatterInterface $date_formatter, TimeInterface $time) {
58 $this->nodeStorage = $node_storage;
59 $this->dateFormatter = $date_formatter;
66 public static function create(ContainerInterface $container) {
68 $container->get('entity.manager')->getStorage('node'),
69 $container->get('date.formatter'),
70 $container->get('datetime.time')
77 public function getFormId() {
78 return 'node_revision_revert_confirm';
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())]);
91 public function getCancelUrl() {
92 return new Url('entity.node.version_history', ['node' => $this->revision->id()]);
98 public function getConfirmText() {
105 public function getDescription() {
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);
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();
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();
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()]
142 * Prepares a revision to be reverted.
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.
149 * @return \Drupal\node\NodeInterface
150 * The prepared revision ready to be stored.
152 protected function prepareRevertedRevision(NodeInterface $revision, FormStateInterface $form_state) {
153 $revision->setNewRevision();
154 $revision->isDefaultRevision(TRUE);