3 namespace Drupal\system\Form;
5 use Drupal\Core\Config\PreExistingConfigException;
6 use Drupal\Core\Config\UnmetDependenciesException;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Extension\ModuleInstallerInterface;
9 use Drupal\Core\Form\ConfirmFormBase;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Builds a confirmation form for enabling modules with dependencies.
20 class ModulesListConfirmForm extends ConfirmFormBase {
23 * The module handler service.
25 * @var \Drupal\Core\Extension\ModuleHandlerInterface
27 protected $moduleHandler;
30 * The expirable key value store.
32 * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
34 protected $keyValueExpirable;
37 * An associative list of modules to enable or disable.
41 protected $modules = [];
44 * The module installer.
46 * @var \Drupal\Core\Extension\ModuleInstallerInterface
48 protected $moduleInstaller;
51 * Constructs a ModulesListConfirmForm object.
53 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
55 * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
56 * The module installer.
57 * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable
58 * The key value expirable factory.
60 public function __construct(ModuleHandlerInterface $module_handler, ModuleInstallerInterface $module_installer, KeyValueStoreExpirableInterface $key_value_expirable) {
61 $this->moduleHandler = $module_handler;
62 $this->moduleInstaller = $module_installer;
63 $this->keyValueExpirable = $key_value_expirable;
69 public static function create(ContainerInterface $container) {
71 $container->get('module_handler'),
72 $container->get('module_installer'),
73 $container->get('keyvalue.expirable')->get('module_list')
80 public function getQuestion() {
81 return $this->t('Some required modules must be enabled');
87 public function getCancelUrl() {
88 return new Url('system.modules_list');
94 public function getConfirmText() {
95 return $this->t('Continue');
101 public function getDescription() {
102 return $this->t('Would you like to continue with the above?');
108 public function getFormId() {
109 return 'system_modules_confirm_form';
115 public function buildForm(array $form, FormStateInterface $form_state) {
116 $account = $this->currentUser()->id();
117 $this->modules = $this->keyValueExpirable->get($account);
119 // Redirect to the modules list page if the key value store is empty.
120 if (!$this->modules) {
121 return $this->redirect('system.modules_list');
124 $items = $this->buildMessageList();
126 '#theme' => 'item_list',
130 return parent::buildForm($form, $form_state);
134 * Builds the message list for the confirmation form.
136 * @return \Drupal\Component\Render\MarkupInterface[]
137 * Array of markup for the list of messages on the form.
139 * @see \Drupal\system\Form\ModulesListForm::buildModuleList()
141 protected function buildMessageList() {
143 if (!empty($this->modules['dependencies'])) {
144 // Display a list of required modules that have to be installed as well
145 // but were not manually selected.
146 foreach ($this->modules['dependencies'] as $module => $dependencies) {
147 $items[] = $this->formatPlural(count($dependencies), 'You must enable the @required module to install @module.', 'You must enable the @required modules to install @module.', [
148 '@module' => $this->modules['install'][$module],
149 // It is safe to implode this because module names are not translated
150 // markup and so will not be double-escaped.
151 '@required' => implode(', ', $dependencies),
161 public function submitForm(array &$form, FormStateInterface $form_state) {
162 // Remove the key value store entry.
163 $account = $this->currentUser()->id();
164 $this->keyValueExpirable->delete($account);
166 if (!empty($this->modules['install'])) {
167 // Don't catch the exception that this can throw for missing dependencies:
168 // the form doesn't allow modules with unmet dependencies, so the only way
169 // this can happen is if the filesystem changed between form display and
170 // submit, in which case the user has bigger problems.
172 // Install the given modules.
173 $this->moduleInstaller->install(array_keys($this->modules['install']));
175 catch (PreExistingConfigException $e) {
176 $config_objects = $e->flattenConfigObjects($e->getConfigObjects());
179 count($config_objects),
180 'Unable to install @extension, %config_names already exists in active configuration.',
181 'Unable to install @extension, %config_names already exist in active configuration.',
183 '%config_names' => implode(', ', $config_objects),
184 '@extension' => $this->modules['install'][$e->getExtension()]
190 catch (UnmetDependenciesException $e) {
192 $e->getTranslatedMessage($this->getStringTranslation(), $this->modules['install'][$e->getExtension()]),
198 $module_names = array_values($this->modules['install']);
199 drupal_set_message($this->formatPlural(count($module_names), 'Module %name has been enabled.', '@count modules have been enabled: %names.', [
200 '%name' => $module_names[0],
201 '%names' => implode(', ', $module_names),
205 $form_state->setRedirectUrl($this->getCancelUrl());