ConfigRevertConfirmForm.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 reverting configuration.
  12. */
  13. class ConfigRevertConfirmForm extends ConfirmFormBase {
  14. /**
  15. * The type of config being reverted.
  16. *
  17. * @var string
  18. */
  19. protected $type;
  20. /**
  21. * The name of the config item being reverted, 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 ConfigRevertConfirmForm 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_update_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 revert (as opposed to import), the configuration item must exist in
  81. // both active storage and extension storage, so check that and make a 404
  82. // error if not.
  83. $extension = $this->configRevert->getFromExtension($this->type, $this->name);
  84. $active = $this->configRevert->getFromActive($this->type, $this->name);
  85. if (!$extension || !$active) {
  86. throw new NotFoundHttpException();
  87. }
  88. return $this->t('Are you sure you want to revert the %type config %item to its source configuration?', ['%type' => $type_label, '%item' => $this->name]);
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getCancelUrl() {
  94. return new Url('config_update_ui.report');
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function getDescription() {
  100. return $this->t('Customizations will be lost. This action cannot be undone.');
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getConfirmText() {
  106. return $this->t('Revert');
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function buildForm(array $form, FormStateInterface $form_state, $config_type = NULL, $config_name = NULL) {
  112. $this->type = $config_type;
  113. $this->name = $config_name;
  114. $form = parent::buildForm($form, $form_state);
  115. return $form;
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function submitForm(array &$form, FormStateInterface $form_state) {
  121. $this->configRevert->revert($this->type, $this->name);
  122. $this->messenger()->addMessage($this->t('The configuration %item has been reverted to its source.', ['%item' => $this->name]));
  123. $form_state->setRedirectUrl($this->getCancelUrl());
  124. }
  125. }