NodeRevisionRevertForm.php 3.26 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\node\Form\NodeRevisionDeleteForm.
 */

namespace Drupal\node\Form;

10
use Drupal\Core\Entity\EntityStorageInterface;
11
use Drupal\Core\Form\ConfirmFormBase;
12
use Drupal\Core\Url;
13 14 15 16 17 18
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a form for reverting a node revision.
 */
19
class NodeRevisionRevertForm extends ConfirmFormBase {
20 21 22 23 24 25 26 27 28 29 30

  /**
   * The node revision.
   *
   * @var \Drupal\node\NodeInterface
   */
  protected $revision;

  /**
   * The node storage.
   *
31
   * @var \Drupal\Core\Entity\EntityStorageInterface
32 33 34 35 36 37
   */
  protected $nodeStorage;

  /**
   * Constructs a new NodeRevisionRevertForm.
   *
38
   * @param \Drupal\Core\Entity\EntityStorageInterface $node_storage
39 40
   *   The node storage.
   */
41
  public function __construct(EntityStorageInterface $node_storage) {
42 43 44 45 46 47 48 49
    $this->nodeStorage = $node_storage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
50
      $container->get('entity.manager')->getStorage('node')
51 52 53 54 55 56
    );
  }

  /**
   * {@inheritdoc}
   */
57
  public function getFormId() {
58 59 60 61 62 63 64 65 66 67 68 69 70
    return 'node_revision_revert_confirm';
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return t('Are you sure you want to revert to the revision from %revision-date?', array('%revision-date' => format_date($this->revision->getRevisionCreationTime())));
  }

  /**
   * {@inheritdoc}
   */
71 72
  public function getCancelUrl() {
    return new Url('node.revision_overview', array('node' => $this->revision->id()));
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return t('Revert');
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return '';
  }

  /**
   * {@inheritdoc}
   */
92
  public function buildForm(array $form, array &$form_state, $node_revision = NULL) {
93
    $this->revision = $this->nodeStorage->loadRevision($node_revision);
94 95 96
    $form = parent::buildForm($form, $form_state);

    return $form;
97 98 99 100 101 102 103 104 105 106 107 108 109 110
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, array &$form_state) {
    $this->revision->setNewRevision();
    // Make this the new default revision for the node.
    $this->revision->isDefaultRevision(TRUE);

    // The revision timestamp will be updated when the revision is saved. Keep the
    // original one for the confirmation message.
    $original_revision_timestamp = $this->revision->getRevisionCreationTime();

111
    $this->revision->revision_log = t('Copy of the revision from %date.', array('%date' => format_date($original_revision_timestamp)));
112 113 114 115 116

    $this->revision->save();

    watchdog('content', '@type: reverted %title revision %revision.', array('@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()));
    drupal_set_message(t('@type %title has been reverted back to the revision from %revision-date.', array('@type' => node_get_type_label($this->revision), '%title' => $this->revision->label(), '%revision-date' => format_date($original_revision_timestamp))));
117 118 119 120 121 122
    $form_state['redirect_route'] = array(
      'route_name' => 'node.revision_overview',
      'route_parameters' => array(
        'node' => $this->revision->id(),
      ),
    );
123 124 125
  }

}