Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / src / Form / ModulesListForm.php
1 <?php
2
3 namespace Drupal\system\Form;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Config\PreExistingConfigException;
7 use Drupal\Core\Config\UnmetDependenciesException;
8 use Drupal\Core\Access\AccessManagerInterface;
9 use Drupal\Core\Extension\Extension;
10 use Drupal\Core\Extension\InfoParserException;
11 use Drupal\Core\Extension\ModuleHandlerInterface;
12 use Drupal\Core\Extension\ModuleInstallerInterface;
13 use Drupal\Core\Form\FormBase;
14 use Drupal\Core\Form\FormStateInterface;
15 use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
16 use Drupal\Core\Render\Element;
17 use Drupal\Core\Session\AccountInterface;
18 use Drupal\user\PermissionHandlerInterface;
19 use Drupal\Core\Url;
20 use Symfony\Component\DependencyInjection\ContainerInterface;
21
22 /**
23  * Provides module installation interface.
24  *
25  * The list of modules gets populated by module.info.yml files, which contain
26  * each module's name, description, and information about which modules it
27  * requires. See \Drupal\Core\Extension\InfoParser for info on module.info.yml
28  * descriptors.
29  *
30  * @internal
31  */
32 class ModulesListForm extends FormBase {
33
34   /**
35    * The current user.
36    *
37    * @var \Drupal\Core\Session\AccountInterface
38    */
39   protected $currentUser;
40
41   /**
42    * The module handler service.
43    *
44    * @var \Drupal\Core\Extension\ModuleHandlerInterface
45    */
46   protected $moduleHandler;
47
48   /**
49    * The expirable key value store.
50    *
51    * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
52    */
53   protected $keyValueExpirable;
54
55   /**
56    * The module installer.
57    *
58    * @var \Drupal\Core\Extension\ModuleInstallerInterface
59    */
60   protected $moduleInstaller;
61
62   /**
63    * The permission handler.
64    *
65    * @var \Drupal\user\PermissionHandlerInterface
66    */
67   protected $permissionHandler;
68
69   /**
70    * {@inheritdoc}
71    */
72   public static function create(ContainerInterface $container) {
73     return new static(
74       $container->get('module_handler'),
75       $container->get('module_installer'),
76       $container->get('keyvalue.expirable')->get('module_list'),
77       $container->get('access_manager'),
78       $container->get('current_user'),
79       $container->get('user.permissions')
80     );
81   }
82
83   /**
84    * Constructs a ModulesListForm object.
85    *
86    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
87    *   The module handler.
88    * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
89    *   The module installer.
90    * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable
91    *   The key value expirable factory.
92    * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
93    *   Access manager.
94    * @param \Drupal\Core\Session\AccountInterface $current_user
95    *   The current user.
96    * @param \Drupal\user\PermissionHandlerInterface $permission_handler
97    *   The permission handler.
98    */
99   public function __construct(ModuleHandlerInterface $module_handler, ModuleInstallerInterface $module_installer, KeyValueStoreExpirableInterface $key_value_expirable, AccessManagerInterface $access_manager, AccountInterface $current_user, PermissionHandlerInterface $permission_handler) {
100     $this->moduleHandler = $module_handler;
101     $this->moduleInstaller = $module_installer;
102     $this->keyValueExpirable = $key_value_expirable;
103     $this->accessManager = $access_manager;
104     $this->currentUser = $current_user;
105     $this->permissionHandler = $permission_handler;
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function getFormId() {
112     return 'system_modules';
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public function buildForm(array $form, FormStateInterface $form_state) {
119     require_once DRUPAL_ROOT . '/core/includes/install.inc';
120     $distribution = drupal_install_profile_distribution_name();
121
122     // Include system.admin.inc so we can use the sort callbacks.
123     $this->moduleHandler->loadInclude('system', 'inc', 'system.admin');
124
125     $form['filters'] = [
126       '#type' => 'container',
127       '#attributes' => [
128         'class' => ['table-filter', 'js-show'],
129       ],
130     ];
131
132     $form['filters']['text'] = [
133       '#type' => 'search',
134       '#title' => $this->t('Filter modules'),
135       '#title_display' => 'invisible',
136       '#size' => 30,
137       '#placeholder' => $this->t('Filter by name or description'),
138       '#description' => $this->t('Enter a part of the module name or description'),
139       '#attributes' => [
140         'class' => ['table-filter-text'],
141         'data-table' => '#system-modules',
142         'autocomplete' => 'off',
143       ],
144     ];
145
146     // Sort all modules by their names.
147     try {
148       $modules = system_rebuild_module_data();
149       uasort($modules, 'system_sort_modules_by_info_name');
150     }
151     catch (InfoParserException $e) {
152       $this->messenger()->addError($this->t('Modules could not be listed due to an error: %error', ['%error' => $e->getMessage()]));
153       $modules = [];
154     }
155
156     // Iterate over each of the modules.
157     $form['modules']['#tree'] = TRUE;
158     foreach ($modules as $filename => $module) {
159       if (empty($module->info['hidden'])) {
160         $package = $module->info['package'];
161         $form['modules'][$package][$filename] = $this->buildRow($modules, $module, $distribution);
162         $form['modules'][$package][$filename]['#parents'] = ['modules', $filename];
163       }
164     }
165
166     // Add a wrapper around every package.
167     foreach (Element::children($form['modules']) as $package) {
168       $form['modules'][$package] += [
169         '#type' => 'details',
170         '#title' => $this->t($package),
171         '#open' => TRUE,
172         '#theme' => 'system_modules_details',
173         '#attributes' => ['class' => ['package-listing']],
174         // Ensure that the "Core" package comes first.
175         '#weight' => $package == 'Core' ? -10 : NULL,
176       ];
177     }
178
179     // If testing modules are shown, collapse the corresponding package by
180     // default.
181     if (isset($form['modules']['Testing'])) {
182       $form['modules']['Testing']['#open'] = FALSE;
183     }
184
185     // Lastly, sort all packages by title.
186     uasort($form['modules'], ['\Drupal\Component\Utility\SortArray', 'sortByTitleProperty']);
187
188     $form['#attached']['library'][] = 'core/drupal.tableresponsive';
189     $form['#attached']['library'][] = 'system/drupal.system.modules';
190     $form['actions'] = ['#type' => 'actions'];
191     $form['actions']['submit'] = [
192       '#type' => 'submit',
193       '#value' => $this->t('Install'),
194       '#button_type' => 'primary',
195     ];
196
197     return $form;
198   }
199
200   /**
201    * Builds a table row for the system modules page.
202    *
203    * @param array $modules
204    *   The list existing modules.
205    * @param \Drupal\Core\Extension\Extension $module
206    *   The module for which to build the form row.
207    * @param $distribution
208    *
209    * @return array
210    *   The form row for the given module.
211    */
212   protected function buildRow(array $modules, Extension $module, $distribution) {
213     // Set the basic properties.
214     $row['#required'] = [];
215     $row['#requires'] = [];
216     $row['#required_by'] = [];
217
218     $row['name']['#markup'] = $module->info['name'];
219     $row['description']['#markup'] = $this->t($module->info['description']);
220     $row['version']['#markup'] = $module->info['version'];
221
222     // Generate link for module's help page. Assume that if a hook_help()
223     // implementation exists then the module provides an overview page, rather
224     // than checking to see if the page exists, which is costly.
225     if ($this->moduleHandler->moduleExists('help') && $module->status && in_array($module->getName(), $this->moduleHandler->getImplementations('help'))) {
226       $row['links']['help'] = [
227         '#type' => 'link',
228         '#title' => $this->t('Help'),
229         '#url' => Url::fromRoute('help.page', ['name' => $module->getName()]),
230         '#options' => ['attributes' => ['class' => ['module-link', 'module-link-help'], 'title' => $this->t('Help')]],
231       ];
232     }
233
234     // Generate link for module's permission, if the user has access to it.
235     if ($module->status && $this->currentUser->hasPermission('administer permissions') && $this->permissionHandler->moduleProvidesPermissions($module->getName())) {
236       $row['links']['permissions'] = [
237         '#type' => 'link',
238         '#title' => $this->t('Permissions'),
239         '#url' => Url::fromRoute('user.admin_permissions'),
240         '#options' => ['fragment' => 'module-' . $module->getName(), 'attributes' => ['class' => ['module-link', 'module-link-permissions'], 'title' => $this->t('Configure permissions')]],
241       ];
242     }
243
244     // Generate link for module's configuration page, if it has one.
245     if ($module->status && isset($module->info['configure'])) {
246       $route_parameters = isset($module->info['configure_parameters']) ? $module->info['configure_parameters'] : [];
247       if ($this->accessManager->checkNamedRoute($module->info['configure'], $route_parameters, $this->currentUser)) {
248         $row['links']['configure'] = [
249           '#type' => 'link',
250           '#title' => $this->t('Configure <span class="visually-hidden">the @module module</span>', ['@module' => $module->info['name']]),
251           '#url' => Url::fromRoute($module->info['configure'], $route_parameters),
252           '#options' => [
253             'attributes' => [
254               'class' => ['module-link', 'module-link-configure'],
255             ],
256           ],
257         ];
258       }
259     }
260
261     // Present a checkbox for installing and indicating the status of a module.
262     $row['enable'] = [
263       '#type' => 'checkbox',
264       '#title' => $this->t('Install'),
265       '#default_value' => (bool) $module->status,
266       '#disabled' => (bool) $module->status,
267     ];
268
269     // Disable the checkbox for required modules.
270     if (!empty($module->info['required'])) {
271       // Used when displaying modules that are required by the installation profile
272       $row['enable']['#disabled'] = TRUE;
273       $row['#required_by'][] = $distribution . (!empty($module->info['explanation']) ? ' (' . $module->info['explanation'] . ')' : '');
274     }
275
276     // Check the compatibilities.
277     $compatible = TRUE;
278
279     // Initialize an empty array of reasons why the module is incompatible. Add
280     // each reason as a separate element of the array.
281     $reasons = [];
282
283     // Check the core compatibility.
284     if ($module->info['core'] != \Drupal::CORE_COMPATIBILITY) {
285       $compatible = FALSE;
286       $reasons[] = $this->t('This version is not compatible with Drupal @core_version and should be replaced.', [
287         '@core_version' => \Drupal::CORE_COMPATIBILITY,
288       ]);
289     }
290
291     // Ensure this module is compatible with the currently installed version of PHP.
292     if (version_compare(phpversion(), $module->info['php']) < 0) {
293       $compatible = FALSE;
294       $required = $module->info['php'] . (substr_count($module->info['php'], '.') < 2 ? '.*' : '');
295       $reasons[] = $this->t('This module requires PHP version @php_required and is incompatible with PHP version @php_version.', [
296         '@php_required' => $required,
297         '@php_version' => phpversion(),
298       ]);
299     }
300
301     // If this module is not compatible, disable the checkbox.
302     if (!$compatible) {
303       $status = implode(' ', $reasons);
304       $row['enable']['#disabled'] = TRUE;
305       $row['description']['#markup'] = $status;
306       $row['#attributes']['class'][] = 'incompatible';
307     }
308
309     // If this module requires other modules, add them to the array.
310     foreach ($module->requires as $dependency => $version) {
311       if (!isset($modules[$dependency])) {
312         $row['#requires'][$dependency] = $this->t('@module (<span class="admin-missing">missing</span>)', ['@module' => Unicode::ucfirst($dependency)]);
313         $row['enable']['#disabled'] = TRUE;
314       }
315       // Only display visible modules.
316       elseif (empty($modules[$dependency]->hidden)) {
317         $name = $modules[$dependency]->info['name'];
318         // Disable the module's checkbox if it is incompatible with the
319         // dependency's version.
320         if ($incompatible_version = drupal_check_incompatibility($version, str_replace(\Drupal::CORE_COMPATIBILITY . '-', '', $modules[$dependency]->info['version']))) {
321           $row['#requires'][$dependency] = $this->t('@module (<span class="admin-missing">incompatible with</span> version @version)', [
322             '@module' => $name . $incompatible_version,
323             '@version' => $modules[$dependency]->info['version'],
324           ]);
325           $row['enable']['#disabled'] = TRUE;
326         }
327         // Disable the checkbox if the dependency is incompatible with this
328         // version of Drupal core.
329         elseif ($modules[$dependency]->info['core'] != \Drupal::CORE_COMPATIBILITY) {
330           $row['#requires'][$dependency] = $this->t('@module (<span class="admin-missing">incompatible with</span> this version of Drupal core)', [
331             '@module' => $name,
332           ]);
333           $row['enable']['#disabled'] = TRUE;
334         }
335         elseif ($modules[$dependency]->status) {
336           $row['#requires'][$dependency] = $this->t('@module', ['@module' => $name]);
337         }
338         else {
339           $row['#requires'][$dependency] = $this->t('@module (<span class="admin-disabled">disabled</span>)', ['@module' => $name]);
340         }
341       }
342     }
343
344     // If this module is required by other modules, list those, and then make it
345     // impossible to disable this one.
346     foreach ($module->required_by as $dependent => $version) {
347       if (isset($modules[$dependent]) && empty($modules[$dependent]->info['hidden'])) {
348         if ($modules[$dependent]->status == 1 && $module->status == 1) {
349           $row['#required_by'][$dependent] = $this->t('@module', ['@module' => $modules[$dependent]->info['name']]);
350           $row['enable']['#disabled'] = TRUE;
351         }
352         else {
353           $row['#required_by'][$dependent] = $this->t('@module (<span class="admin-disabled">disabled</span>)', ['@module' => $modules[$dependent]->info['name']]);
354         }
355       }
356     }
357
358     return $row;
359   }
360
361   /**
362    * Helper function for building a list of modules to install.
363    *
364    * @param \Drupal\Core\Form\FormStateInterface $form_state
365    *   The current state of the form.
366    *
367    * @return array
368    *   An array of modules to install and their dependencies.
369    */
370   protected function buildModuleList(FormStateInterface $form_state) {
371     // Build a list of modules to install.
372     $modules = [
373       'install' => [],
374       'dependencies' => [],
375       'experimental' => [],
376     ];
377
378     $data = system_rebuild_module_data();
379     foreach ($data as $name => $module) {
380       // If the module is installed there is nothing to do.
381       if ($this->moduleHandler->moduleExists($name)) {
382         continue;
383       }
384       // Required modules have to be installed.
385       if (!empty($module->required)) {
386         $modules['install'][$name] = $module->info['name'];
387       }
388       // Selected modules should be installed.
389       elseif (($checkbox = $form_state->getValue(['modules', $name], FALSE)) && $checkbox['enable']) {
390         $modules['install'][$name] = $data[$name]->info['name'];
391         // Identify experimental modules.
392         if ($data[$name]->info['package'] == 'Core (Experimental)') {
393           $modules['experimental'][$name] = $data[$name]->info['name'];
394         }
395       }
396     }
397
398     // Add all dependencies to a list.
399     foreach ($modules['install'] as $module => $value) {
400       foreach (array_keys($data[$module]->requires) as $dependency) {
401         if (!isset($modules['install'][$dependency]) && !$this->moduleHandler->moduleExists($dependency)) {
402           $modules['dependencies'][$module][$dependency] = $data[$dependency]->info['name'];
403           $modules['install'][$dependency] = $data[$dependency]->info['name'];
404
405           // Identify experimental modules.
406           if ($data[$dependency]->info['package'] == 'Core (Experimental)') {
407             $modules['experimental'][$dependency] = $data[$dependency]->info['name'];
408           }
409         }
410       }
411     }
412
413     // Make sure the install API is available.
414     include_once DRUPAL_ROOT . '/core/includes/install.inc';
415
416     // Invoke hook_requirements('install'). If failures are detected, make
417     // sure the dependent modules aren't installed either.
418     foreach (array_keys($modules['install']) as $module) {
419       if (!drupal_check_module($module)) {
420         unset($modules['install'][$module]);
421         unset($modules['experimental'][$module]);
422         foreach (array_keys($data[$module]->required_by) as $dependent) {
423           unset($modules['install'][$dependent]);
424           unset($modules['dependencies'][$dependent]);
425         }
426       }
427     }
428
429     return $modules;
430   }
431
432   /**
433    * {@inheritdoc}
434    */
435   public function submitForm(array &$form, FormStateInterface $form_state) {
436     // Retrieve a list of modules to install and their dependencies.
437     $modules = $this->buildModuleList($form_state);
438
439     // Redirect to a confirmation form if needed.
440     if (!empty($modules['experimental']) || !empty($modules['dependencies'])) {
441
442       $route_name = !empty($modules['experimental']) ? 'system.modules_list_experimental_confirm' : 'system.modules_list_confirm';
443       // Write the list of changed module states into a key value store.
444       $account = $this->currentUser()->id();
445       $this->keyValueExpirable->setWithExpire($account, $modules, 60);
446
447       // Redirect to the confirmation form.
448       $form_state->setRedirect($route_name);
449
450       // We can exit here because at least one modules has dependencies
451       // which we have to prompt the user for in a confirmation form.
452       return;
453     }
454
455     // Install the given modules.
456     if (!empty($modules['install'])) {
457       try {
458         $this->moduleInstaller->install(array_keys($modules['install']));
459         $module_names = array_values($modules['install']);
460         $this->messenger()->addStatus($this->formatPlural(count($module_names), 'Module %name has been enabled.', '@count modules have been enabled: %names.', [
461           '%name' => $module_names[0],
462           '%names' => implode(', ', $module_names),
463         ]));
464       }
465       catch (PreExistingConfigException $e) {
466         $config_objects = $e->flattenConfigObjects($e->getConfigObjects());
467         $this->messenger()->addError(
468           $this->formatPlural(
469             count($config_objects),
470             'Unable to install @extension, %config_names already exists in active configuration.',
471             'Unable to install @extension, %config_names already exist in active configuration.',
472             [
473               '%config_names' => implode(', ', $config_objects),
474               '@extension' => $modules['install'][$e->getExtension()],
475             ])
476         );
477         return;
478       }
479       catch (UnmetDependenciesException $e) {
480         $this->messenger()->addError(
481           $e->getTranslatedMessage($this->getStringTranslation(), $modules['install'][$e->getExtension()])
482         );
483         return;
484       }
485     }
486   }
487
488 }