ConfigDeleteConfirmForm.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. /**
  11. * Defines a confirmation form for deleting configuration.
  12. */
  13. class ConfigDeleteConfirmForm extends ConfirmFormBase {
  14. /**
  15. * The type of config being deleted.
  16. *
  17. * @var string
  18. */
  19. protected $type;
  20. /**
  21. * The name of the config item being deleted, without the prefix.
  22. *
  23. * @var string
  24. */
  25. protected $name;
  26. /**
  27. * The config lister.
  28. *
  29. * @var \Drupal\config_update\ConfigListInterface
  30. */
  31. protected $configList;
  32. /**
  33. * The config reverter.
  34. *
  35. * @var \Drupal\config_update\ConfigRevertInterface
  36. */
  37. protected $configRevert;
  38. /**
  39. * Constructs a ConfigDeleteConfirmForm object.
  40. *
  41. * @param \Drupal\config_update\ConfigListInterface $config_list
  42. * The config lister.
  43. * @param \Drupal\config_update\ConfigRevertInterface $config_update
  44. * The config reverter.
  45. */
  46. public function __construct(ConfigListInterface $config_list, ConfigRevertInterface $config_update) {
  47. $this->configList = $config_list;
  48. $this->configRevert = $config_update;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public static function create(ContainerInterface $container) {
  54. return new static(
  55. $container->get('config_update.config_list'),
  56. $container->get('config_update.config_update')
  57. );
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getFormId() {
  63. return 'config_delete_confirm';
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function getQuestion() {
  69. if ($this->type == 'system.simple') {
  70. $type_label = $this->t('Simple configuration');
  71. }
  72. else {
  73. $definition = $this->configList->getType($this->type);
  74. if (!$definition) {
  75. // Make a 404 error if the type doesn't exist.
  76. throw new NotFoundHttpException();
  77. }
  78. $type_label = $definition->get('label');
  79. }
  80. // To delete, the configuration item must exist in active storage. Check
  81. // that and make a 404 error if not.
  82. $active = $this->configRevert->getFromActive($this->type, $this->name);
  83. if (!$active) {
  84. throw new NotFoundHttpException();
  85. }
  86. return $this->t('Are you sure you want to delete the %type config %item?', ['%type' => $type_label, '%item' => $this->name]);
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function getCancelUrl() {
  92. return new Url('config_update_ui.report');
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function getDescription() {
  98. 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.');
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function getConfirmText() {
  104. return $this->t('Delete');
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function buildForm(array $form, FormStateInterface $form_state, $config_type = NULL, $config_name = NULL) {
  110. $this->type = $config_type;
  111. $this->name = $config_name;
  112. $form = parent::buildForm($form, $form_state);
  113. return $form;
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function submitForm(array &$form, FormStateInterface $form_state) {
  119. $this->configRevert->delete($this->type, $this->name);
  120. $this->messenger()->addMessage($this->t('The configuration %item has been deleted.', ['%item' => $this->name]));
  121. $form_state->setRedirectUrl($this->getCancelUrl());
  122. }
  123. }