View source
<?php
namespace Drupal\thunder\Installer\Form;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Extension\Extension;
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\Installer\InstallerKernel;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Drupal\system\ModuleDependencyMessageTrait;
use Drupal\user\PermissionHandlerInterface;
use Drupal\Component\Utility\Environment;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ModuleConfigureForm extends FormBase {
use ModuleDependencyMessageTrait;
protected $moduleExtensionList;
protected $moduleInstaller;
protected $accessManager;
protected $moduleHandler;
protected $currentUser;
protected $permissionHandler;
public static function create(ContainerInterface $container) {
$form = parent::create($container);
$form
->setModuleExtensionList($container
->get('extension.list.module'));
$form
->setModuleInstaller($container
->get('module_installer'));
$form
->setAccessManager($container
->get('access_manager'));
$form
->setCurrentUser($container
->get('current_user'));
$form
->setModuleHandler($container
->get('module_handler'));
$form
->setPermissionHandler($container
->get('user.permissions'));
$form
->setConfigFactory($container
->get('config.factory'));
return $form;
}
protected function setModuleExtensionList(ModuleExtensionList $moduleExtensionList) {
$this->moduleExtensionList = $moduleExtensionList;
}
protected function setModuleInstaller(ModuleInstallerInterface $moduleInstaller) {
$this->moduleInstaller = $moduleInstaller;
}
protected function setAccessManager(AccessManagerInterface $accessManager) {
$this->accessManager = $accessManager;
}
protected function setCurrentUser(AccountProxyInterface $accountProxy) {
$this->currentUser = $accountProxy;
}
protected function setModuleHandler(ModuleHandlerInterface $moduleHandler) {
$this->moduleHandler = $moduleHandler;
}
protected function setPermissionHandler(PermissionHandlerInterface $handler) {
$this->permissionHandler = $handler;
}
public function getFormId() {
return 'thunder_module_configure_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['description'] = [
'#type' => 'item',
'#markup' => $this
->t('This is a list of modules that are supported by Thunder, but not enabled by default.'),
];
$form['install_modules'] = [
'#type' => 'container',
'#tree' => TRUE,
];
$modules = $this->moduleExtensionList
->getList();
$thunder_features = array_filter($modules, function (Extension $module) {
return $module->info['package'] === 'Thunder Optional';
});
foreach ($thunder_features as $id => $module) {
$form['install_modules'][$id] = [
'#type' => 'container',
];
$form['install_modules'][$id]['enable'] = [
'#type' => 'checkbox',
'#title' => $module->info['name'],
'#default_value' => $module->status,
'#disabled' => $module->status,
];
$form['install_modules'][$id]['info'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'module-info',
],
],
];
$form['install_modules'][$id]['info']['description'] = [
'#markup' => '<span class="text module-description">' . $module->info['description'] . '</span>',
];
$requires = [];
foreach ($module->requires as $dependency => $dependency_object) {
if ($incompatible = $this
->checkDependencyMessage($modules, $dependency, $dependency_object)) {
$requires[$dependency] = $incompatible;
$form['install_modules'][$id]['enable']['#disabled'] = TRUE;
continue;
}
$name = $modules[$dependency]->info['name'];
$requires[$dependency] = $modules[$dependency]->status ? $this
->t('@module', [
'@module' => $name,
]) : $this
->t('@module (<span class="admin-disabled">disabled</span>)', [
'@module' => $name,
]);
}
$form['install_modules'][$id]['info']['requires'] = [
'#prefix' => '<div class="admin-requirements">Requires: ',
'#suffix' => '</div>',
'#theme' => 'item_list',
'#items' => $requires,
'#context' => [
'list_style' => 'comma-list',
],
];
if ($module->status) {
if ($this->moduleHandler
->moduleExists('help') && in_array($module
->getName(), $this->moduleHandler
->getImplementations('help'))) {
$form['install_modules'][$id]['info']['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 ($this->currentUser
->hasPermission('administer permissions') && $this->permissionHandler
->moduleProvidesPermissions($module
->getName())) {
$form['install_modules'][$id]['info']['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 (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)) {
$form['install_modules'][$id]['info']['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',
],
],
],
];
}
}
}
}
$form['#title'] = $this
->t('Install & configure modules');
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['save'] = [
'#type' => 'submit',
'#value' => $this
->t('Save and continue'),
'#button_type' => 'primary',
];
$form['#attached']['library'][] = 'thunder/module.configure.form';
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$operations = [];
foreach ($form_state
->getValue('install_modules') as $module => $values) {
$extension = $this->moduleExtensionList
->get($module);
if (!$extension->status && $values['enable']) {
$operations[] = [
[
$this,
'batchOperation',
],
[
$module,
],
];
}
}
if ($operations) {
$batch = [
'operations' => $operations,
'title' => $this
->t('Installing additional modules'),
'error_message' => $this
->t('The installation has encountered an error.'),
];
if (InstallerKernel::installationAttempted()) {
$buildInfo = $form_state
->getBuildInfo();
$buildInfo['args'][0]['thunder_install_batch'] = $batch;
$form_state
->setBuildInfo($buildInfo);
}
else {
batch_set($batch);
}
}
}
public function batchOperation($module, array &$context) {
Environment::setTimeLimit(0);
$this->moduleInstaller
->install([
$module,
]);
}
}