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