NodeTypeDeleteConfirm.php 2.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php

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

namespace Drupal\node\Form;

use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Database\Connection;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a form for content type deletion.
 */
17
class NodeTypeDeleteConfirm extends EntityConfirmFormBase {
18
19
20
21
22
23
24
25
26
27
28
29
30
31

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * Constructs a new NodeTypeDeleteConfirm object.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection.
   */
32
  public function __construct(Connection $database) {
33
34
35
36
37
38
    $this->database = $database;
  }

  /**
   * {@inheritdoc}
   */
39
  public static function create(ContainerInterface $container) {
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    return new static(
      $container->get('database')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return t('Are you sure you want to delete the content type %type?', array('%type' => $this->entity->label()));
  }

  /**
   * {@inheritdoc}
   */
55
56
  public function getCancelRoute() {
    return array(
57
      'route_name' => 'node.overview_types',
58
    );
59
60
61
62
63
64
65
66
67
68
69
70
  }

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

  /**
   * {@inheritdoc}
   */
71
  public function buildForm(array $form, array &$form_state) {
72
73
74
    $num_nodes = $this->database->query("SELECT COUNT(*) FROM {node} WHERE type = :type", array(':type' => $this->entity->id()))->fetchField();
    if ($num_nodes) {
      $caption = '<p>' . format_plural($num_nodes, '%type is used by 1 piece of content on your site. You can not remove this content type until you have removed all of the %type content.', '%type is used by @count pieces of content on your site. You may not remove %type until you have removed all of the %type content.', array('%type' => $this->entity->label())) . '</p>';
75
      $form['#title'] = $this->getQuestion();
76
77
78
79
      $form['description'] = array('#markup' => $caption);
      return $form;
    }

80
    return parent::buildForm($form, $form_state);
81
82
83
84
85
86
87
88
89
90
91
  }

  /**
   * {@inheritdoc}
   */
  public function submit(array $form, array &$form_state) {
    $this->entity->delete();
    $t_args = array('%name' => $this->entity->label());
    drupal_set_message(t('The content type %name has been deleted.', $t_args));
    watchdog('node', 'Deleted content type %name.', $t_args, WATCHDOG_NOTICE);

92
    $form_state['redirect'] = 'admin/structure/types';
93
94
95
  }

}