Upgraded drupal core with security updates
[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 class ModulesUninstallForm extends FormBase {
16
17   /**
18    * The module handler service.
19    *
20    * @var \Drupal\Core\Extension\ModuleHandlerInterface
21    */
22   protected $moduleHandler;
23
24   /**
25    * The module installer service.
26    *
27    * @var \Drupal\Core\Extension\ModuleInstallerInterface
28    */
29   protected $moduleInstaller;
30
31   /**
32    * The expirable key value store.
33    *
34    * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
35    */
36   protected $keyValueExpirable;
37
38   /**
39    * {@inheritdoc}
40    */
41   public static function create(ContainerInterface $container) {
42     return new static(
43       $container->get('module_handler'),
44       $container->get('module_installer'),
45       $container->get('keyvalue.expirable')->get('modules_uninstall')
46     );
47   }
48
49   /**
50    * Constructs a ModulesUninstallForm object.
51    *
52    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
53    *   The module handler.
54    * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
55    *   The module installer.
56    * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable
57    *   The key value expirable factory.
58    */
59   public function __construct(ModuleHandlerInterface $module_handler, ModuleInstallerInterface $module_installer, KeyValueStoreExpirableInterface $key_value_expirable) {
60     $this->moduleHandler = $module_handler;
61     $this->moduleInstaller = $module_installer;
62     $this->keyValueExpirable = $key_value_expirable;
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function getFormId() {
69     return 'system_modules_uninstall';
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function buildForm(array $form, FormStateInterface $form_state) {
76     // Make sure the install API is available.
77     include_once DRUPAL_ROOT . '/core/includes/install.inc';
78
79     // Get a list of all available modules.
80     $modules = system_rebuild_module_data();
81     $uninstallable = array_filter($modules, function ($module) use ($modules) {
82       return empty($modules[$module->getName()]->info['required']) && $module->status;
83     });
84
85     // Include system.admin.inc so we can use the sort callbacks.
86     $this->moduleHandler->loadInclude('system', 'inc', 'system.admin');
87
88     $form['filters'] = [
89       '#type' => 'container',
90       '#attributes' => [
91         'class' => ['table-filter', 'js-show'],
92       ],
93     ];
94
95     $form['filters']['text'] = [
96       '#type' => 'search',
97       '#title' => $this->t('Filter modules'),
98       '#title_display' => 'invisible',
99       '#size' => 30,
100       '#placeholder' => $this->t('Filter by name or description'),
101       '#description' => $this->t('Enter a part of the module name or description'),
102       '#attributes' => [
103         'class' => ['table-filter-text'],
104         'data-table' => '#system-modules-uninstall',
105         'autocomplete' => 'off',
106       ],
107     ];
108
109     $form['modules'] = [];
110
111     // Only build the rest of the form if there are any modules available to
112     // uninstall;
113     if (empty($uninstallable)) {
114       return $form;
115     }
116
117     $profile = drupal_get_profile();
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. (The installation profile
144       // is excluded from this list.)
145       foreach (array_keys($module->required_by) as $dependent) {
146         if ($dependent != $profile && drupal_get_installed_schema_version($dependent) != SCHEMA_UNINSTALLED) {
147           $name = isset($modules[$dependent]->info['name']) ? $modules[$dependent]->info['name'] : $dependent;
148           $form['modules'][$module->getName()]['#required_by'][] = $name;
149           $form['uninstall'][$module->getName()]['#disabled'] = TRUE;
150         }
151       }
152     }
153
154     $form['#attached']['library'][] = 'system/drupal.system.modules';
155     $form['actions'] = ['#type' => 'actions'];
156     $form['actions']['submit'] = [
157       '#type' => 'submit',
158       '#value' => $this->t('Uninstall'),
159     ];
160
161     return $form;
162   }
163
164   /**
165    * {@inheritdoc}
166    */
167   public function validateForm(array &$form, FormStateInterface $form_state) {
168     // Form submitted, but no modules selected.
169     if (!array_filter($form_state->getValue('uninstall'))) {
170       $form_state->setErrorByName('', $this->t('No modules selected.'));
171       $form_state->setRedirect('system.modules_uninstall');
172     }
173   }
174
175   /**
176    * {@inheritdoc}
177    */
178   public function submitForm(array &$form, FormStateInterface $form_state) {
179     // Save all the values in an expirable key value store.
180     $modules = $form_state->getValue('uninstall');
181     $uninstall = array_keys(array_filter($modules));
182     $account = $this->currentUser()->id();
183     // Store the values for 6 hours. This expiration time is also used in
184     // the form cache.
185     $this->keyValueExpirable->setWithExpire($account, $uninstall, 6 * 60 * 60);
186
187     // Redirect to the confirm form.
188     $form_state->setRedirect('system.modules_uninstall_confirm');
189   }
190
191 }