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.
17 class NodeRevisionDeleteForm extends ConfirmFormBase {
22 * @var \Drupal\node\NodeInterface
29 * @var \Drupal\Core\Entity\EntityStorageInterface
31 protected $nodeStorage;
34 * The node type storage.
36 * @var \Drupal\Core\Entity\EntityStorageInterface
38 protected $nodeTypeStorage;
41 * The database connection.
43 * @var \Drupal\Core\Database\Connection
45 protected $connection;
48 * Constructs a new NodeRevisionDeleteForm.
50 * @param \Drupal\Core\Entity\EntityStorageInterface $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.
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;
66 public static function create(ContainerInterface $container) {
67 $entity_manager = $container->get('entity.manager');
69 $entity_manager->getStorage('node'),
70 $entity_manager->getStorage('node_type'),
71 $container->get('database')
78 public function getFormId() {
79 return 'node_revision_delete_confirm';
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())]);
92 public function getCancelUrl() {
93 return new Url('entity.node.version_history', ['node' => $this->revision->id()]);
99 public function getConfirmText() {
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);
116 public function submitForm(array &$form, FormStateInterface $form_state) {
117 $this->nodeStorage->deleteRevision($this->revision->getRevisionId());
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();
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(),
127 $form_state->setRedirect(
128 'entity.node.canonical',
129 ['node' => $this->revision->id()]
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()]