Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[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  * @internal
16  */
17 class NodeRevisionDeleteForm extends ConfirmFormBase {
18
19   /**
20    * The node revision.
21    *
22    * @var \Drupal\node\NodeInterface
23    */
24   protected $revision;
25
26   /**
27    * The node storage.
28    *
29    * @var \Drupal\Core\Entity\EntityStorageInterface
30    */
31   protected $nodeStorage;
32
33   /**
34    * The node type storage.
35    *
36    * @var \Drupal\Core\Entity\EntityStorageInterface
37    */
38   protected $nodeTypeStorage;
39
40   /**
41    * The database connection.
42    *
43    * @var \Drupal\Core\Database\Connection
44    */
45   protected $connection;
46
47   /**
48    * Constructs a new NodeRevisionDeleteForm.
49    *
50    * @param \Drupal\Core\Entity\EntityStorageInterface $node_storage
51    *   The node storage.
52    * @param \Drupal\Core\Entity\EntityStorageInterface $node_type_storage
53    *   The node type storage.
54    * @param \Drupal\Core\Database\Connection $connection
55    *   The database connection.
56    */
57   public function __construct(EntityStorageInterface $node_storage, EntityStorageInterface $node_type_storage, Connection $connection) {
58     $this->nodeStorage = $node_storage;
59     $this->nodeTypeStorage = $node_type_storage;
60     $this->connection = $connection;
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public static function create(ContainerInterface $container) {
67     $entity_manager = $container->get('entity.manager');
68     return new static(
69       $entity_manager->getStorage('node'),
70       $entity_manager->getStorage('node_type'),
71       $container->get('database')
72     );
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function getFormId() {
79     return 'node_revision_delete_confirm';
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function getQuestion() {
86     return t('Are you sure you want to delete the revision from %revision-date?', ['%revision-date' => format_date($this->revision->getRevisionCreationTime())]);
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function getCancelUrl() {
93     return new Url('entity.node.version_history', ['node' => $this->revision->id()]);
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public function getConfirmText() {
100     return t('Delete');
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function buildForm(array $form, FormStateInterface $form_state, $node_revision = NULL) {
107     $this->revision = $this->nodeStorage->loadRevision($node_revision);
108     $form = parent::buildForm($form, $form_state);
109
110     return $form;
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public function submitForm(array &$form, FormStateInterface $form_state) {
117     $this->nodeStorage->deleteRevision($this->revision->getRevisionId());
118
119     $this->logger('content')->notice('@type: deleted %title revision %revision.', ['@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
120     $node_type = $this->nodeTypeStorage->load($this->revision->bundle())->label();
121     $this->messenger()
122       ->addStatus($this->t('Revision from %revision-date of @type %title has been deleted.', [
123         '%revision-date' => format_date($this->revision->getRevisionCreationTime()),
124         '@type' => $node_type,
125         '%title' => $this->revision->label(),
126       ]));
127     $form_state->setRedirect(
128       'entity.node.canonical',
129       ['node' => $this->revision->id()]
130     );
131     if ($this->connection->query('SELECT COUNT(DISTINCT vid) FROM {node_field_revision} WHERE nid = :nid', [':nid' => $this->revision->id()])->fetchField() > 1) {
132       $form_state->setRedirect(
133         'entity.node.version_history',
134         ['node' => $this->revision->id()]
135       );
136     }
137   }
138
139 }