ConfigDeleteConfirmForm.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Drupal\config_update_ui\Form;
  3. use Drupal\Core\Form\ConfirmFormBase;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\Core\Url;
  6. use Drupal\config_update\ConfigListInterface;
  7. use Drupal\config_update\ConfigRevertInterface;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. /**
  10. * Defines a confirmation form for deleting configuration.
  11. */
  12. class ConfigDeleteConfirmForm extends ConfirmFormBase {
  13. /**
  14. * The type of config being deleted.
  15. *
  16. * @var string
  17. */
  18. protected $type;
  19. /**
  20. * The name of the config item being deleted, without the prefix.
  21. *
  22. * @var string
  23. */
  24. protected $name;
  25. /**
  26. * The config lister.
  27. *
  28. * @var \Drupal\config_update\ConfigListInterface
  29. */
  30. protected $configList;
  31. /**
  32. * The config reverter.
  33. *
  34. * @var \Drupal\config_update\ConfigRevertInterface
  35. */
  36. protected $configRevert;
  37. /**
  38. * Constructs a ConfigDeleteConfirmForm object.
  39. *
  40. * @param \Drupal\config_update\ConfigListInterface $config_list
  41. * The config lister.
  42. * @param \Drupal\config_update\ConfigRevertInterface $config_update
  43. * The config reverter.
  44. */
  45. public function __construct(ConfigListInterface $config_list, ConfigRevertInterface $config_update) {
  46. $this->configList = $config_list;
  47. $this->configRevert = $config_update;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public static function create(ContainerInterface $container) {
  53. return new static(
  54. $container->get('config_update.config_list'),
  55. $container->get('config_update.config_update')
  56. );
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function getFormId() {
  62. return 'config_delete_confirm';
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function getQuestion() {
  68. if ($this->type == 'system.simple') {
  69. $type_label = $this->t('Simple configuration');
  70. }
  71. else {
  72. $definition = $this->configList->getType($this->type);
  73. $type_label = $definition->get('label');
  74. }
  75. return $this->t('Are you sure you want to delete the %type config %item?', ['%type' => $type_label, '%item' => $this->name]);
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getCancelUrl() {
  81. return new Url('config_update_ui.report');
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function getDescription() {
  87. return $this->t('This action cannot be undone. Manually deleting configuration from this page can cause problems on your site due to missing dependencies, and should only be done if there is no other way to delete a problematic piece of configuration.');
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getConfirmText() {
  93. return $this->t('Delete');
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function buildForm(array $form, FormStateInterface $form_state, $config_type = NULL, $config_name = NULL) {
  99. $this->type = $config_type;
  100. $this->name = $config_name;
  101. $form = parent::buildForm($form, $form_state);
  102. return $form;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function validateForm(array &$form, FormStateInterface $form_state) {
  108. $value = $this->configRevert->getFromActive($this->type, $this->name);
  109. if (!$value) {
  110. $form_state->setErrorByName('', $this->t('There is no configuration @type named @name to delete', ['@type' => $this->type, '@name' => $this->name]));
  111. return;
  112. }
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function submitForm(array &$form, FormStateInterface $form_state) {
  118. $this->configRevert->delete($this->type, $this->name);
  119. drupal_set_message($this->t('The configuration was deleted.'));
  120. $form_state->setRedirectUrl($this->getCancelUrl());
  121. }
  122. }