Same filename and directory in other branches
- 8.9.x core/modules/system/src/Form/ModulesListConfirmForm.php
- 9 core/modules/system/src/Form/ModulesListConfirmForm.php
Namespace
Drupal\system\Form
File
core/modules/system/src/Form/ModulesListConfirmForm.php
View source
<?php
namespace Drupal\system\Form;
use Drupal\Core\Config\PreExistingConfigException;
use Drupal\Core\Config\UnmetDependenciesException;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ModuleInstallerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ModulesListConfirmForm extends ConfirmFormBase {
use ModulesEnabledTrait;
protected $moduleHandler;
protected $keyValueExpirable;
protected $modules = [];
protected $moduleInstaller;
public function __construct(ModuleHandlerInterface $module_handler, ModuleInstallerInterface $module_installer, KeyValueStoreExpirableInterface $key_value_expirable) {
$this->moduleHandler = $module_handler;
$this->moduleInstaller = $module_installer;
$this->keyValueExpirable = $key_value_expirable;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('module_handler'), $container
->get('module_installer'), $container
->get('keyvalue.expirable')
->get('module_list'));
}
public function getQuestion() {
return $this
->t('Some required modules must be installed');
}
public function getCancelUrl() {
return new Url('system.modules_list');
}
public function getConfirmText() {
return $this
->t('Continue');
}
public function getDescription() {
return $this
->t('Would you like to continue with the above?');
}
public function getFormId() {
return 'system_modules_confirm_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$account = $this
->currentUser()
->id();
$this->modules = $this->keyValueExpirable
->get($account);
if (!$this->modules) {
return $this
->redirect('system.modules_list');
}
$items = $this
->buildMessageList();
$form['message'] = [
'#theme' => 'item_list',
'#items' => $items,
];
return parent::buildForm($form, $form_state);
}
protected function buildMessageList() {
$items = [];
if (!empty($this->modules['dependencies'])) {
foreach ($this->modules['dependencies'] as $module => $dependencies) {
$items[] = $this
->formatPlural(count($dependencies), 'You must install the @required module to install @module.', 'You must install the @required modules to install @module.', [
'@module' => $this->modules['install'][$module],
'@required' => implode(', ', $dependencies),
]);
}
}
return $items;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$account = $this
->currentUser()
->id();
$this->keyValueExpirable
->delete($account);
if (!empty($this->modules['install'])) {
try {
$this->moduleInstaller
->install(array_keys($this->modules['install']));
} catch (PreExistingConfigException $e) {
$this
->messenger()
->addError($this
->modulesFailToEnableMessage($this->modules, $e));
return;
} catch (UnmetDependenciesException $e) {
$this
->messenger()
->addError($e
->getTranslatedMessage($this
->getStringTranslation(), $this->modules['install'][$e
->getExtension()]));
return;
}
$this->messenger = NULL;
$this
->messenger()
->addStatus($this
->modulesEnabledConfirmationMessage($this->modules['install']));
}
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}
Classes