Version 1
[yaffs-website] / web / core / modules / node / src / Form / NodeRevisionDeleteForm.php
1 <?php
2
3 namespace Drupal\node\Form;
4
5 use Drupal\Core\Database\Connection;
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 Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Provides a form for reverting a node revision.
14  */
15 class NodeRevisionDeleteForm extends ConfirmFormBase {
16
17   /**
18    * The node revision.
19    *
20    * @var \Drupal\node\NodeInterface
21    */
22   protected $revision;
23
24   /**
25    * The node storage.
26    *
27    * @var \Drupal\Core\Entity\EntityStorageInterface
28    */
29   protected $nodeStorage;
30
31   /**
32    * The node type storage.
33    *
34    * @var \Drupal\Core\Entity\EntityStorageInterface
35    */
36   protected $nodeTypeStorage;
37
38   /**
39    * The database connection.
40    *
41    * @var \Drupal\Core\Database\Connection
42    */
43   protected $connection;
44
45   /**
46    * Constructs a new NodeRevisionDeleteForm.
47    *
48    * @param \Drupal\Core\Entity\EntityStorageInterface $node_storage
49    *   The 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.
54    */
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;
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public static function create(ContainerInterface $container) {
65     $entity_manager = $container->get('entity.manager');
66     return new static(
67       $entity_manager->getStorage('node'),
68       $entity_manager->getStorage('node_type'),
69       $container->get('database')
70     );
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function getFormId() {
77     return 'node_revision_delete_confirm';
78   }
79
80   /**
81    * {@inheritdoc}
82    */
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())]);
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function getCancelUrl() {
91     return new Url('entity.node.version_history', ['node' => $this->revision->id()]);
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function getConfirmText() {
98     return t('Delete');
99   }
100
101   /**
102    * {@inheritdoc}
103    */
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);
107
108     return $form;
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public function submitForm(array &$form, FormStateInterface $form_state) {
115     $this->nodeStorage->deleteRevision($this->revision->getRevisionId());
116
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()]
123     );
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()]
128       );
129     }
130   }
131
132 }