ModulesUninstallConfirmForm.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Drupal\system\Form;
  3. use Drupal\Core\Config\ConfigManagerInterface;
  4. use Drupal\Core\Config\Entity\ConfigDependencyDeleteFormTrait;
  5. use Drupal\Core\Entity\EntityManagerInterface;
  6. use Drupal\Core\Extension\ModuleInstallerInterface;
  7. use Drupal\Core\Form\ConfirmFormBase;
  8. use Drupal\Core\Form\FormStateInterface;
  9. use Drupal\Core\Url;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
  12. /**
  13. * Builds a confirmation form to uninstall selected modules.
  14. *
  15. * @internal
  16. */
  17. class ModulesUninstallConfirmForm extends ConfirmFormBase {
  18. use ConfigDependencyDeleteFormTrait;
  19. /**
  20. * The module installer service.
  21. *
  22. * @var \Drupal\Core\Extension\ModuleInstallerInterface
  23. */
  24. protected $moduleInstaller;
  25. /**
  26. * The expirable key value store.
  27. *
  28. * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
  29. */
  30. protected $keyValueExpirable;
  31. /**
  32. * The configuration manager.
  33. *
  34. * @var \Drupal\Core\Config\ConfigManagerInterface
  35. */
  36. protected $configManager;
  37. /**
  38. * The entity manager.
  39. *
  40. * @var \Drupal\Core\Entity\EntityManagerInterface
  41. */
  42. protected $entityManager;
  43. /**
  44. * An array of modules to uninstall.
  45. *
  46. * @var array
  47. */
  48. protected $modules = [];
  49. /**
  50. * Constructs a ModulesUninstallConfirmForm object.
  51. *
  52. * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
  53. * The module installer.
  54. * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable
  55. * The key value expirable factory.
  56. * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
  57. * The configuration manager.
  58. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
  59. * The entity manager.
  60. */
  61. public function __construct(ModuleInstallerInterface $module_installer, KeyValueStoreExpirableInterface $key_value_expirable, ConfigManagerInterface $config_manager, EntityManagerInterface $entity_manager) {
  62. $this->moduleInstaller = $module_installer;
  63. $this->keyValueExpirable = $key_value_expirable;
  64. $this->configManager = $config_manager;
  65. $this->entityManager = $entity_manager;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public static function create(ContainerInterface $container) {
  71. return new static(
  72. $container->get('module_installer'),
  73. $container->get('keyvalue.expirable')->get('modules_uninstall'),
  74. $container->get('config.manager'),
  75. $container->get('entity.manager')
  76. );
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function getQuestion() {
  82. return $this->t('Confirm uninstall');
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function getConfirmText() {
  88. return $this->t('Uninstall');
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getCancelUrl() {
  94. return new Url('system.modules_uninstall');
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function getDescription() {
  100. return $this->t('Would you like to continue with uninstalling the above?');
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getFormId() {
  106. return 'system_modules_uninstall_confirm_form';
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function buildForm(array $form, FormStateInterface $form_state) {
  112. // Retrieve the list of modules from the key value store.
  113. $account = $this->currentUser()->id();
  114. $this->modules = $this->keyValueExpirable->get($account);
  115. // Prevent this page from showing when the module list is empty.
  116. if (empty($this->modules)) {
  117. $this->messenger()->addError($this->t('The selected modules could not be uninstalled, either due to a website problem or due to the uninstall confirmation form timing out. Please try again.'));
  118. return $this->redirect('system.modules_uninstall');
  119. }
  120. $data = system_rebuild_module_data();
  121. $form['text']['#markup'] = '<p>' . $this->t('The following modules will be completely uninstalled from your site, and <em>all data from these modules will be lost</em>!') . '</p>';
  122. $form['modules'] = [
  123. '#theme' => 'item_list',
  124. '#items' => array_map(function ($module) use ($data) {
  125. return $data[$module]->info['name'];
  126. }, $this->modules),
  127. ];
  128. // List the dependent entities.
  129. $this->addDependencyListsToForm($form, 'module', $this->modules, $this->configManager, $this->entityManager);
  130. return parent::buildForm($form, $form_state);
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function submitForm(array &$form, FormStateInterface $form_state) {
  136. // Clear the key value store entry.
  137. $account = $this->currentUser()->id();
  138. $this->keyValueExpirable->delete($account);
  139. // Uninstall the modules.
  140. $this->moduleInstaller->uninstall($this->modules);
  141. $this->messenger()->addStatus($this->t('The selected modules have been uninstalled.'));
  142. $form_state->setRedirectUrl($this->getCancelUrl());
  143. }
  144. }