View source
<?php
namespace Drupal\system\Form;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ModuleInstallerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ModulesUninstallForm extends FormBase {
protected $moduleHandler;
protected $moduleInstaller;
protected $keyValueExpirable;
protected $moduleExtensionList;
public static function create(ContainerInterface $container) {
return new static($container
->get('module_handler'), $container
->get('module_installer'), $container
->get('keyvalue.expirable')
->get('modules_uninstall'), $container
->get('extension.list.module'));
}
public function __construct(ModuleHandlerInterface $module_handler, ModuleInstallerInterface $module_installer, KeyValueStoreExpirableInterface $key_value_expirable, ModuleExtensionList $extension_list_module) {
$this->moduleExtensionList = $extension_list_module;
$this->moduleHandler = $module_handler;
$this->moduleInstaller = $module_installer;
$this->keyValueExpirable = $key_value_expirable;
}
public function getFormId() {
return 'system_modules_uninstall';
}
public function buildForm(array $form, FormStateInterface $form_state) {
include_once DRUPAL_ROOT . '/core/includes/install.inc';
$uninstallable = array_filter($this->moduleExtensionList
->getList(), function ($module) {
return empty($module->info['required']) && $module->status;
});
$this->moduleHandler
->loadInclude('system', 'inc', 'system.admin');
$form['filters'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'table-filter',
'js-show',
],
],
];
$form['filters']['text'] = [
'#type' => 'search',
'#title' => $this
->t('Filter modules'),
'#title_display' => 'invisible',
'#size' => 30,
'#placeholder' => $this
->t('Filter by name or description'),
'#description' => $this
->t('Enter a part of the module name or description'),
'#attributes' => [
'class' => [
'table-filter-text',
],
'data-table' => '#system-modules-uninstall',
'autocomplete' => 'off',
],
];
$form['modules'] = [];
if (empty($uninstallable)) {
return $form;
}
uasort($uninstallable, 'system_sort_modules_by_info_name');
$validation_reasons = $this->moduleInstaller
->validateUninstall(array_keys($uninstallable));
$form['uninstall'] = [
'#tree' => TRUE,
];
foreach ($uninstallable as $module_key => $module) {
$name = $module->info['name'] ?: $module
->getName();
$form['modules'][$module
->getName()]['#module_name'] = $name;
$form['modules'][$module
->getName()]['name']['#markup'] = $name;
$form['modules'][$module
->getName()]['description']['#markup'] = $this
->t($module->info['description']);
$form['uninstall'][$module
->getName()] = [
'#type' => 'checkbox',
'#title' => $this
->t('Uninstall @module module', [
'@module' => $name,
]),
'#title_display' => 'invisible',
];
if (isset($validation_reasons[$module_key])) {
$form['modules'][$module
->getName()]['#validation_reasons'] = $validation_reasons[$module_key];
$form['uninstall'][$module
->getName()]['#disabled'] = TRUE;
}
foreach (array_keys($module->required_by) as $dependent) {
if (drupal_get_installed_schema_version($dependent) != SCHEMA_UNINSTALLED) {
$name = isset($modules[$dependent]->info['name']) ? $modules[$dependent]->info['name'] : $dependent;
$form['modules'][$module
->getName()]['#required_by'][] = $name;
$form['uninstall'][$module
->getName()]['#disabled'] = TRUE;
}
}
}
$form['#attached']['library'][] = 'system/drupal.system.modules';
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Uninstall'),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (!array_filter($form_state
->getValue('uninstall'))) {
$form_state
->setErrorByName('', $this
->t('No modules selected.'));
$form_state
->setRedirect('system.modules_uninstall');
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$modules = $form_state
->getValue('uninstall');
$uninstall = array_keys(array_filter($modules));
$account = $this
->currentUser()
->id();
$this->keyValueExpirable
->setWithExpire($account, $uninstall, 6 * 60 * 60);
$form_state
->setRedirect('system.modules_uninstall_confirm');
}
}