3 namespace Drupal\system\Form;
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\Core\Extension\ModuleInstallerInterface;
7 use Drupal\Core\Form\FormBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Provides a form for uninstalling modules.
17 class ModulesUninstallForm extends FormBase {
20 * The module handler service.
22 * @var \Drupal\Core\Extension\ModuleHandlerInterface
24 protected $moduleHandler;
27 * The module installer service.
29 * @var \Drupal\Core\Extension\ModuleInstallerInterface
31 protected $moduleInstaller;
34 * The expirable key value store.
36 * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
38 protected $keyValueExpirable;
43 public static function create(ContainerInterface $container) {
45 $container->get('module_handler'),
46 $container->get('module_installer'),
47 $container->get('keyvalue.expirable')->get('modules_uninstall')
52 * Constructs a ModulesUninstallForm object.
54 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
56 * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
57 * The module installer.
58 * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable
59 * The key value expirable factory.
61 public function __construct(ModuleHandlerInterface $module_handler, ModuleInstallerInterface $module_installer, KeyValueStoreExpirableInterface $key_value_expirable) {
62 $this->moduleHandler = $module_handler;
63 $this->moduleInstaller = $module_installer;
64 $this->keyValueExpirable = $key_value_expirable;
70 public function getFormId() {
71 return 'system_modules_uninstall';
77 public function buildForm(array $form, FormStateInterface $form_state) {
78 // Make sure the install API is available.
79 include_once DRUPAL_ROOT . '/core/includes/install.inc';
81 // Get a list of all available modules.
82 $modules = system_rebuild_module_data();
83 $uninstallable = array_filter($modules, function ($module) use ($modules) {
84 return empty($modules[$module->getName()]->info['required']) && $module->status;
87 // Include system.admin.inc so we can use the sort callbacks.
88 $this->moduleHandler->loadInclude('system', 'inc', 'system.admin');
91 '#type' => 'container',
93 'class' => ['table-filter', 'js-show'],
97 $form['filters']['text'] = [
99 '#title' => $this->t('Filter modules'),
100 '#title_display' => 'invisible',
102 '#placeholder' => $this->t('Filter by name or description'),
103 '#description' => $this->t('Enter a part of the module name or description'),
105 'class' => ['table-filter-text'],
106 'data-table' => '#system-modules-uninstall',
107 'autocomplete' => 'off',
111 $form['modules'] = [];
113 // Only build the rest of the form if there are any modules available to
115 if (empty($uninstallable)) {
119 $profile = drupal_get_profile();
121 // Sort all modules by their name.
122 uasort($uninstallable, 'system_sort_modules_by_info_name');
123 $validation_reasons = $this->moduleInstaller->validateUninstall(array_keys($uninstallable));
125 $form['uninstall'] = ['#tree' => TRUE];
126 foreach ($uninstallable as $module_key => $module) {
127 $name = $module->info['name'] ?: $module->getName();
128 $form['modules'][$module->getName()]['#module_name'] = $name;
129 $form['modules'][$module->getName()]['name']['#markup'] = $name;
130 $form['modules'][$module->getName()]['description']['#markup'] = $this->t($module->info['description']);
132 $form['uninstall'][$module->getName()] = [
133 '#type' => 'checkbox',
134 '#title' => $this->t('Uninstall @module module', ['@module' => $name]),
135 '#title_display' => 'invisible',
138 // If a validator returns reasons not to uninstall a module,
139 // list the reasons and disable the check box.
140 if (isset($validation_reasons[$module_key])) {
141 $form['modules'][$module->getName()]['#validation_reasons'] = $validation_reasons[$module_key];
142 $form['uninstall'][$module->getName()]['#disabled'] = TRUE;
144 // All modules which depend on this one must be uninstalled first, before
145 // we can allow this module to be uninstalled. (The installation profile
146 // is excluded from this list.)
147 foreach (array_keys($module->required_by) as $dependent) {
148 if ($dependent != $profile && drupal_get_installed_schema_version($dependent) != SCHEMA_UNINSTALLED) {
149 $name = isset($modules[$dependent]->info['name']) ? $modules[$dependent]->info['name'] : $dependent;
150 $form['modules'][$module->getName()]['#required_by'][] = $name;
151 $form['uninstall'][$module->getName()]['#disabled'] = TRUE;
156 $form['#attached']['library'][] = 'system/drupal.system.modules';
157 $form['actions'] = ['#type' => 'actions'];
158 $form['actions']['submit'] = [
160 '#value' => $this->t('Uninstall'),
169 public function validateForm(array &$form, FormStateInterface $form_state) {
170 // Form submitted, but no modules selected.
171 if (!array_filter($form_state->getValue('uninstall'))) {
172 $form_state->setErrorByName('', $this->t('No modules selected.'));
173 $form_state->setRedirect('system.modules_uninstall');
180 public function submitForm(array &$form, FormStateInterface $form_state) {
181 // Save all the values in an expirable key value store.
182 $modules = $form_state->getValue('uninstall');
183 $uninstall = array_keys(array_filter($modules));
184 $account = $this->currentUser()->id();
185 // Store the values for 6 hours. This expiration time is also used in
187 $this->keyValueExpirable->setWithExpire($account, $uninstall, 6 * 60 * 60);
189 // Redirect to the confirm form.
190 $form_state->setRedirect('system.modules_uninstall_confirm');