View source
<?php
namespace Drupal\easy_install\Form;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Extension\Extension;
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 Drupal\Core\Render\Element;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\PermissionHandlerInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PurgeConfigurationsForm extends FormBase {
protected $currentUser;
protected $moduleHandler;
protected $keyValueExpirable;
protected $moduleInstaller;
protected $permissionHandler;
public static function create(ContainerInterface $container) {
return new static($container
->get('module_handler'), $container
->get('module_installer'), $container
->get('keyvalue.expirable')
->get('easy_install_purgeconfigs'), $container
->get('access_manager'), $container
->get('current_user'), $container
->get('user.permissions'));
}
public function __construct(ModuleHandlerInterface $module_handler, ModuleInstallerInterface $module_installer, KeyValueStoreExpirableInterface $key_value_expirable, AccessManagerInterface $access_manager, AccountInterface $current_user, PermissionHandlerInterface $permission_handler) {
$this->moduleHandler = $module_handler;
$this->moduleInstaller = $module_installer;
$this->keyValueExpirable = $key_value_expirable;
$this->accessManager = $access_manager;
$this->currentUser = $current_user;
$this->permissionHandler = $permission_handler;
}
public function getFormId() {
return 'system_modules';
}
public function buildForm(array $form, FormStateInterface $form_state) {
require_once DRUPAL_ROOT . '/core/includes/install.inc';
$distribution = drupal_install_profile_distribution_name();
$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 uninstalled module name
or description to delete its garbage configuration'),
'#attributes' => [
'class' => [
'table-filter-text',
],
'data-table' => '#system-modules',
'autocomplete' => 'off',
],
];
$modules = system_rebuild_module_data();
uasort($modules, 'system_sort_modules_by_info_name');
$form['modules']['#tree'] = TRUE;
foreach ($modules as $filename => $module) {
if (empty($module->info['hidden']) && !$module->status) {
$package = $module->info['package'];
$form['modules'][$package][$filename] = $this
->buildRow($modules, $module, $distribution);
$form['modules'][$package][$filename]['#parents'] = [
'modules',
$filename,
];
}
}
foreach (Element::children($form['modules']) as $package) {
$form['modules'][$package] += [
'#type' => 'details',
'#title' => $this
->t('@package', [
'@package' => $package,
]),
'#open' => TRUE,
'#theme' => 'system_modules_details',
'#attributes' => [
'class' => [
'package-listing',
],
],
'#weight' => $package == 'Core' ? -10 : NULL,
];
}
if (isset($form['modules']['Testing'])) {
$form['modules']['Testing']['#open'] = FALSE;
}
uasort($form['modules'], [
'\\Drupal\\Component\\Utility\\SortArray',
'sortByTitleProperty',
]);
$form['#attached']['library'][] = 'system/drupal.system.modules';
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Purge Configurations'),
'#button_type' => 'primary',
];
return $form;
}
protected function buildRow(array $modules, Extension $module, $distribution) {
$row['#required'] = [];
$row['#requires'] = [];
$row['#required_by'] = [];
$row['name']['#markup'] = $module->info['name'];
$row['description']['#markup'] = $this
->t('@desc', [
'@desc' => $module->info['description'],
]);
$row['version']['#markup'] = $module->info['version'];
if ($this->moduleHandler
->moduleExists('help') && $module->status && in_array($module
->getName(), $this->moduleHandler
->getImplementations('help'))) {
$row['links']['help'] = [
'#type' => 'link',
'#title' => $this
->t('Help'),
'#url' => Url::fromRoute('help.page', [
'name' => $module
->getName(),
]),
'#options' => [
'attributes' => [
'class' => [
'module-link',
'module-link-help',
],
'title' => $this
->t('Help'),
],
],
];
}
if ($module->status && $this->currentUser
->hasPermission('administer permissions') && $this->permissionHandler
->moduleProvidesPermissions($module
->getName())) {
$row['links']['permissions'] = [
'#type' => 'link',
'#title' => $this
->t('Permissions'),
'#url' => Url::fromRoute('user.admin_permissions'),
'#options' => [
'fragment' => 'module-' . $module
->getName(),
'attributes' => [
'class' => [
'module-link',
'module-link-permissions',
],
'title' => $this
->t('Configure permissions'),
],
],
];
}
if ($module->status && isset($module->info['configure'])) {
$route_parameters = isset($module->info['configure_parameters']) ? $module->info['configure_parameters'] : [];
if ($this->accessManager
->checkNamedRoute($module->info['configure'], $route_parameters, $this->currentUser)) {
$row['links']['configure'] = [
'#type' => 'link',
'#title' => $this
->t('Configure <span class="visually-hidden">the @module module</span>', [
'@module' => $module->info['name'],
]),
'#url' => Url::fromRoute($module->info['configure'], $route_parameters),
'#options' => [
'attributes' => [
'class' => [
'module-link',
'module-link-configure',
],
],
],
];
}
}
$row['enable'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Install'),
'#default_value' => (bool) $module->status,
'#disabled' => (bool) $module->status,
];
if (!empty($module->info['required'])) {
$row['enable']['#disabled'] = TRUE;
$row['#required_by'][] = $distribution . (!empty($module->info['explanation']) ? ' (' . $module->info['explanation'] . ')' : '');
}
$compatible = TRUE;
$reasons = [];
if ($module->info['core'] != \Drupal::CORE_COMPATIBILITY) {
$compatible = FALSE;
$reasons[] = $this
->t('This version is not compatible with Drupal
@core_version and should be replaced.', [
'@core_version' => \Drupal::CORE_COMPATIBILITY,
]);
}
if (version_compare(phpversion(), $module->info['php']) < 0) {
$compatible = FALSE;
$required = $module->info['php'] . (substr_count($module->info['php'], '.') < 2 ? '.*' : '');
$reasons[] = $this
->t('This module requires PHP version @php_required and
is incompatible with PHP version @php_version.', [
'@php_required' => $required,
'@php_version' => phpversion(),
]);
}
if (!$compatible) {
$status = implode(' ', $reasons);
$row['enable']['#disabled'] = TRUE;
$row['description']['#markup'] = $status;
$row['#attributes']['class'][] = 'incompatible';
}
foreach ($module->requires as $dependency => $version) {
if (!isset($modules[$dependency])) {
$row['#requires'][$dependency] = $this
->t('@module (<span class="admin-missing">missing</span>)', [
'@module' => Unicode::ucfirst($dependency),
]);
$row['enable']['#disabled'] = TRUE;
}
elseif (empty($modules[$dependency]->hidden)) {
$name = $modules[$dependency]->info['name'];
if ($incompatible_version = drupal_check_incompatibility($version, str_replace(\Drupal::CORE_COMPATIBILITY . '-', '', $modules[$dependency]->info['version']))) {
$row['#requires'][$dependency] = $this
->t('@module (<span class="admin-missing">incompatible with</span> version @version)', [
'@module' => $name . $incompatible_version,
'@version' => $modules[$dependency]->info['version'],
]);
$row['enable']['#disabled'] = TRUE;
}
elseif ($modules[$dependency]->info['core'] != \Drupal::CORE_COMPATIBILITY) {
$row['#requires'][$dependency] = $this
->t('@module (<span class="admin-missing">incompatible with</span> this version of Drupal core)', [
'@module' => $name,
]);
$row['enable']['#disabled'] = TRUE;
}
elseif ($modules[$dependency]->status) {
$row['#requires'][$dependency] = $this
->t('@module', [
'@module' => $name,
]);
}
else {
$row['#requires'][$dependency] = $this
->t('@module (<span class="admin-disabled">disabled</span>)', [
'@module' => $name,
]);
}
}
}
foreach ($module->required_by as $dependent => $version) {
if (isset($modules[$dependent]) && empty($modules[$dependent]->info['hidden'])) {
if ($modules[$dependent]->status == 1 && $module->status == 1) {
$row['#required_by'][$dependent] = $this
->t('@module', [
'@module' => $modules[$dependent]->info['name'],
]);
$row['enable']['#disabled'] = TRUE;
}
else {
$row['#required_by'][$dependent] = $this
->t('@module (<span class="admin-disabled">disabled</span>)', [
'@module' => $modules[$dependent]->info['name'],
]);
}
}
}
return $row;
}
protected function buildModuleList(FormStateInterface $form_state) {
$modules = [
'install' => [],
'dependencies' => [],
'experimental' => [],
];
$data = system_rebuild_module_data();
foreach ($data as $name => $module) {
if ($this->moduleHandler
->moduleExists($name)) {
continue;
}
if (!empty($module->required)) {
$modules['install'][$name] = $module->info['name'];
}
elseif (($checkbox = $form_state
->getValue([
'modules',
$name,
], FALSE)) && $checkbox['enable']) {
$modules['install'][$name] = $data[$name]->info['name'];
if ($data[$name]->info['package'] == 'Core (Experimental)') {
$modules['experimental'][$name] = $data[$name]->info['name'];
}
}
}
while (list($module) = each($modules['install'])) {
foreach (array_keys($data[$module]->requires) as $dependency) {
if (!isset($modules['install'][$dependency]) && !$this->moduleHandler
->moduleExists($dependency)) {
$modules['dependencies'][$module][$dependency] = $data[$dependency]->info['name'];
$modules['install'][$dependency] = $data[$dependency]->info['name'];
if ($data[$dependency]->info['package'] == 'Core (Experimental)') {
$modules['experimental'][$dependency] = $data[$dependency]->info['name'];
}
}
}
}
include_once DRUPAL_ROOT . '/core/includes/install.inc';
foreach (array_keys($modules['install']) as $module) {
if (!drupal_check_module($module)) {
unset($modules['install'][$module]);
unset($modules['experimental'][$module]);
foreach (array_keys($data[$module]->required_by) as $dependent) {
unset($modules['install'][$dependent]);
unset($modules['dependencies'][$dependent]);
}
}
}
return $modules;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$modules = $this
->buildModuleList($form_state);
$route_name = 'easy_install.purge_configurations_confirm';
$account = $this
->currentUser()
->id();
$this->keyValueExpirable
->setWithExpire($account, $modules, 60);
$form_state
->setRedirect($route_name);
}
}