ModulesUninstallForm.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace Drupal\system\Form;
  3. use Drupal\Core\Extension\ModuleHandlerInterface;
  4. use Drupal\Core\Extension\ModuleInstallerInterface;
  5. use Drupal\Core\Form\FormBase;
  6. use Drupal\Core\Form\FormStateInterface;
  7. use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. /**
  10. * Provides a form for uninstalling modules.
  11. *
  12. * @internal
  13. */
  14. class ModulesUninstallForm extends FormBase {
  15. /**
  16. * The module handler service.
  17. *
  18. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  19. */
  20. protected $moduleHandler;
  21. /**
  22. * The module installer service.
  23. *
  24. * @var \Drupal\Core\Extension\ModuleInstallerInterface
  25. */
  26. protected $moduleInstaller;
  27. /**
  28. * The expirable key value store.
  29. *
  30. * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
  31. */
  32. protected $keyValueExpirable;
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public static function create(ContainerInterface $container) {
  37. return new static(
  38. $container->get('module_handler'),
  39. $container->get('module_installer'),
  40. $container->get('keyvalue.expirable')->get('modules_uninstall')
  41. );
  42. }
  43. /**
  44. * Constructs a ModulesUninstallForm object.
  45. *
  46. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  47. * The module handler.
  48. * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
  49. * The module installer.
  50. * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable
  51. * The key value expirable factory.
  52. */
  53. public function __construct(ModuleHandlerInterface $module_handler, ModuleInstallerInterface $module_installer, KeyValueStoreExpirableInterface $key_value_expirable) {
  54. $this->moduleHandler = $module_handler;
  55. $this->moduleInstaller = $module_installer;
  56. $this->keyValueExpirable = $key_value_expirable;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function getFormId() {
  62. return 'system_modules_uninstall';
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function buildForm(array $form, FormStateInterface $form_state) {
  68. // Make sure the install API is available.
  69. include_once DRUPAL_ROOT . '/core/includes/install.inc';
  70. // Get a list of all available modules.
  71. $modules = system_rebuild_module_data();
  72. $uninstallable = array_filter($modules, function ($module) use ($modules) {
  73. return empty($modules[$module->getName()]->info['required']) && $module->status;
  74. });
  75. // Include system.admin.inc so we can use the sort callbacks.
  76. $this->moduleHandler->loadInclude('system', 'inc', 'system.admin');
  77. $form['filters'] = [
  78. '#type' => 'container',
  79. '#attributes' => [
  80. 'class' => ['table-filter', 'js-show'],
  81. ],
  82. ];
  83. $form['filters']['text'] = [
  84. '#type' => 'search',
  85. '#title' => $this->t('Filter modules'),
  86. '#title_display' => 'invisible',
  87. '#size' => 30,
  88. '#placeholder' => $this->t('Filter by name or description'),
  89. '#description' => $this->t('Enter a part of the module name or description'),
  90. '#attributes' => [
  91. 'class' => ['table-filter-text'],
  92. 'data-table' => '#system-modules-uninstall',
  93. 'autocomplete' => 'off',
  94. ],
  95. ];
  96. $form['modules'] = [];
  97. // Only build the rest of the form if there are any modules available to
  98. // uninstall;
  99. if (empty($uninstallable)) {
  100. return $form;
  101. }
  102. // Sort all modules by their name.
  103. uasort($uninstallable, 'system_sort_modules_by_info_name');
  104. $validation_reasons = $this->moduleInstaller->validateUninstall(array_keys($uninstallable));
  105. $form['uninstall'] = ['#tree' => TRUE];
  106. foreach ($uninstallable as $module_key => $module) {
  107. $name = $module->info['name'] ?: $module->getName();
  108. $form['modules'][$module->getName()]['#module_name'] = $name;
  109. $form['modules'][$module->getName()]['name']['#markup'] = $name;
  110. $form['modules'][$module->getName()]['description']['#markup'] = $this->t($module->info['description']);
  111. $form['uninstall'][$module->getName()] = [
  112. '#type' => 'checkbox',
  113. '#title' => $this->t('Uninstall @module module', ['@module' => $name]),
  114. '#title_display' => 'invisible',
  115. ];
  116. // If a validator returns reasons not to uninstall a module,
  117. // list the reasons and disable the check box.
  118. if (isset($validation_reasons[$module_key])) {
  119. $form['modules'][$module->getName()]['#validation_reasons'] = $validation_reasons[$module_key];
  120. $form['uninstall'][$module->getName()]['#disabled'] = TRUE;
  121. }
  122. // All modules which depend on this one must be uninstalled first, before
  123. // we can allow this module to be uninstalled.
  124. foreach (array_keys($module->required_by) as $dependent) {
  125. if (drupal_get_installed_schema_version($dependent) != SCHEMA_UNINSTALLED) {
  126. $name = isset($modules[$dependent]->info['name']) ? $modules[$dependent]->info['name'] : $dependent;
  127. $form['modules'][$module->getName()]['#required_by'][] = $name;
  128. $form['uninstall'][$module->getName()]['#disabled'] = TRUE;
  129. }
  130. }
  131. }
  132. $form['#attached']['library'][] = 'system/drupal.system.modules';
  133. $form['actions'] = ['#type' => 'actions'];
  134. $form['actions']['submit'] = [
  135. '#type' => 'submit',
  136. '#value' => $this->t('Uninstall'),
  137. ];
  138. return $form;
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function validateForm(array &$form, FormStateInterface $form_state) {
  144. // Form submitted, but no modules selected.
  145. if (!array_filter($form_state->getValue('uninstall'))) {
  146. $form_state->setErrorByName('', $this->t('No modules selected.'));
  147. $form_state->setRedirect('system.modules_uninstall');
  148. }
  149. }
  150. /**
  151. * {@inheritdoc}
  152. */
  153. public function submitForm(array &$form, FormStateInterface $form_state) {
  154. // Save all the values in an expirable key value store.
  155. $modules = $form_state->getValue('uninstall');
  156. $uninstall = array_keys(array_filter($modules));
  157. $account = $this->currentUser()->id();
  158. // Store the values for 6 hours. This expiration time is also used in
  159. // the form cache.
  160. $this->keyValueExpirable->setWithExpire($account, $uninstall, 6 * 60 * 60);
  161. // Redirect to the confirm form.
  162. $form_state->setRedirect('system.modules_uninstall_confirm');
  163. }
  164. }