3 namespace Drupal\node\Form;
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Form\ConfirmFormBase;
8 use Drupal\Core\Form\FormStateInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Provides a form for reverting a node revision.
15 class NodeRevisionDeleteForm extends ConfirmFormBase {
20 * @var \Drupal\node\NodeInterface
27 * @var \Drupal\Core\Entity\EntityStorageInterface
29 protected $nodeStorage;
32 * The node type storage.
34 * @var \Drupal\Core\Entity\EntityStorageInterface
36 protected $nodeTypeStorage;
39 * The database connection.
41 * @var \Drupal\Core\Database\Connection
43 protected $connection;
46 * Constructs a new NodeRevisionDeleteForm.
48 * @param \Drupal\Core\Entity\EntityStorageInterface $node_storage
50 * @param \Drupal\Core\Entity\EntityStorageInterface $node_type_storage
51 * The node type storage.
52 * @param \Drupal\Core\Database\Connection $connection
53 * The database connection.
55 public function __construct(EntityStorageInterface $node_storage, EntityStorageInterface $node_type_storage, Connection $connection) {
56 $this->nodeStorage = $node_storage;
57 $this->nodeTypeStorage = $node_type_storage;
58 $this->connection = $connection;
64 public static function create(ContainerInterface $container) {
65 $entity_manager = $container->get('entity.manager');
67 $entity_manager->getStorage('node'),
68 $entity_manager->getStorage('node_type'),
69 $container->get('database')
76 public function getFormId() {
77 return 'node_revision_delete_confirm';
83 public function getQuestion() {
84 return t('Are you sure you want to delete the revision from %revision-date?', ['%revision-date' => format_date($this->revision->getRevisionCreationTime())]);
90 public function getCancelUrl() {
91 return new Url('entity.node.version_history', ['node' => $this->revision->id()]);
97 public function getConfirmText() {
104 public function buildForm(array $form, FormStateInterface $form_state, $node_revision = NULL) {
105 $this->revision = $this->nodeStorage->loadRevision($node_revision);
106 $form = parent::buildForm($form, $form_state);
114 public function submitForm(array &$form, FormStateInterface $form_state) {
115 $this->nodeStorage->deleteRevision($this->revision->getRevisionId());
117 $this->logger('content')->notice('@type: deleted %title revision %revision.', ['@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
118 $node_type = $this->nodeTypeStorage->load($this->revision->bundle())->label();
119 drupal_set_message(t('Revision from %revision-date of @type %title has been deleted.', ['%revision-date' => format_date($this->revision->getRevisionCreationTime()), '@type' => $node_type, '%title' => $this->revision->label()]));
120 $form_state->setRedirect(
121 'entity.node.canonical',
122 ['node' => $this->revision->id()]
124 if ($this->connection->query('SELECT COUNT(DISTINCT vid) FROM {node_field_revision} WHERE nid = :nid', [':nid' => $this->revision->id()])->fetchField() > 1) {
125 $form_state->setRedirect(
126 'entity.node.version_history',
127 ['node' => $this->revision->id()]