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.
18 class ModulesListConfirmForm extends ConfirmFormBase {
21 * The module handler service.
23 * @var \Drupal\Core\Extension\ModuleHandlerInterface
25 protected $moduleHandler;
28 * The expirable key value store.
30 * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
32 protected $keyValueExpirable;
35 * An associative list of modules to enable or disable.
39 protected $modules = [];
42 * The module installer.
44 * @var \Drupal\Core\Extension\ModuleInstallerInterface
46 protected $moduleInstaller;
49 * Constructs a ModulesListConfirmForm object.
51 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
53 * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
54 * The module installer.
55 * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable
56 * The key value expirable factory.
58 public function __construct(ModuleHandlerInterface $module_handler, ModuleInstallerInterface $module_installer, KeyValueStoreExpirableInterface $key_value_expirable) {
59 $this->moduleHandler = $module_handler;
60 $this->moduleInstaller = $module_installer;
61 $this->keyValueExpirable = $key_value_expirable;
67 public static function create(ContainerInterface $container) {
69 $container->get('module_handler'),
70 $container->get('module_installer'),
71 $container->get('keyvalue.expirable')->get('module_list')
78 public function getQuestion() {
79 return $this->t('Some required modules must be enabled');
85 public function getCancelUrl() {
86 return new Url('system.modules_list');
92 public function getConfirmText() {
93 return $this->t('Continue');
99 public function getDescription() {
100 return $this->t('Would you like to continue with the above?');
106 public function getFormId() {
107 return 'system_modules_confirm_form';
113 public function buildForm(array $form, FormStateInterface $form_state) {
114 $account = $this->currentUser()->id();
115 $this->modules = $this->keyValueExpirable->get($account);
117 // Redirect to the modules list page if the key value store is empty.
118 if (!$this->modules) {
119 return $this->redirect('system.modules_list');
122 $items = $this->buildMessageList();
124 '#theme' => 'item_list',
128 return parent::buildForm($form, $form_state);
132 * Builds the message list for the confirmation form.
134 * @return \Drupal\Component\Render\MarkupInterface[]
135 * Array of markup for the list of messages on the form.
137 * @see \Drupal\system\Form\ModulesListForm::buildModuleList()
139 protected function buildMessageList() {
141 if (!empty($this->modules['dependencies'])) {
142 // Display a list of required modules that have to be installed as well
143 // but were not manually selected.
144 foreach ($this->modules['dependencies'] as $module => $dependencies) {
145 $items[] = $this->formatPlural(count($dependencies), 'You must enable the @required module to install @module.', 'You must enable the @required modules to install @module.', [
146 '@module' => $this->modules['install'][$module],
147 // It is safe to implode this because module names are not translated
148 // markup and so will not be double-escaped.
149 '@required' => implode(', ', $dependencies),
159 public function submitForm(array &$form, FormStateInterface $form_state) {
160 // Remove the key value store entry.
161 $account = $this->currentUser()->id();
162 $this->keyValueExpirable->delete($account);
164 if (!empty($this->modules['install'])) {
165 // Don't catch the exception that this can throw for missing dependencies:
166 // the form doesn't allow modules with unmet dependencies, so the only way
167 // this can happen is if the filesystem changed between form display and
168 // submit, in which case the user has bigger problems.
170 // Install the given modules.
171 $this->moduleInstaller->install(array_keys($this->modules['install']));
173 catch (PreExistingConfigException $e) {
174 $config_objects = $e->flattenConfigObjects($e->getConfigObjects());
177 count($config_objects),
178 'Unable to install @extension, %config_names already exists in active configuration.',
179 'Unable to install @extension, %config_names already exist in active configuration.',
181 '%config_names' => implode(', ', $config_objects),
182 '@extension' => $this->modules['install'][$e->getExtension()]
188 catch (UnmetDependenciesException $e) {
190 $e->getTranslatedMessage($this->getStringTranslation(), $this->modules['install'][$e->getExtension()]),
196 $module_names = array_values($this->modules['install']);
197 drupal_set_message($this->formatPlural(count($module_names), 'Module %name has been enabled.', '@count modules have been enabled: %names.', [
198 '%name' => $module_names[0],
199 '%names' => implode(', ', $module_names),
203 $form_state->setRedirectUrl($this->getCancelUrl());