Version 1
[yaffs-website] / web / core / modules / system / src / Form / ModulesListConfirmForm.php
1 <?php
2
3 namespace Drupal\system\Form;
4
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;
12 use Drupal\Core\Url;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Builds a confirmation form for enabling modules with dependencies.
17  */
18 class ModulesListConfirmForm extends ConfirmFormBase {
19
20   /**
21    * The module handler service.
22    *
23    * @var \Drupal\Core\Extension\ModuleHandlerInterface
24    */
25   protected $moduleHandler;
26
27   /**
28    * The expirable key value store.
29    *
30    * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
31    */
32   protected $keyValueExpirable;
33
34   /**
35    * An associative list of modules to enable or disable.
36    *
37    * @var array
38    */
39   protected $modules = [];
40
41   /**
42    * The module installer.
43    *
44    * @var \Drupal\Core\Extension\ModuleInstallerInterface
45    */
46   protected $moduleInstaller;
47
48   /**
49    * Constructs a ModulesListConfirmForm object.
50    *
51    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
52    *   The 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.
57    */
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;
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public static function create(ContainerInterface $container) {
68     return new static(
69       $container->get('module_handler'),
70       $container->get('module_installer'),
71       $container->get('keyvalue.expirable')->get('module_list')
72     );
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function getQuestion() {
79     return $this->t('Some required modules must be enabled');
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function getCancelUrl() {
86     return new Url('system.modules_list');
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function getConfirmText() {
93     return $this->t('Continue');
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public function getDescription() {
100     return $this->t('Would you like to continue with the above?');
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function getFormId() {
107     return 'system_modules_confirm_form';
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   public function buildForm(array $form, FormStateInterface $form_state) {
114     $account = $this->currentUser()->id();
115     $this->modules = $this->keyValueExpirable->get($account);
116
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');
120     }
121
122     $items = $this->buildMessageList();
123     $form['message'] = [
124       '#theme' => 'item_list',
125       '#items' => $items,
126     ];
127
128     return parent::buildForm($form, $form_state);
129   }
130
131   /**
132    * Builds the message list for the confirmation form.
133    *
134    * @return \Drupal\Component\Render\MarkupInterface[]
135    *   Array of markup for the list of messages on the form.
136    *
137    * @see \Drupal\system\Form\ModulesListForm::buildModuleList()
138    */
139   protected function buildMessageList() {
140     $items = [];
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),
150         ]);
151       }
152     }
153     return $items;
154   }
155
156   /**
157    * {@inheritdoc}
158    */
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);
163
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.
169       try {
170         // Install the given modules.
171         $this->moduleInstaller->install(array_keys($this->modules['install']));
172       }
173       catch (PreExistingConfigException $e) {
174         $config_objects = $e->flattenConfigObjects($e->getConfigObjects());
175         drupal_set_message(
176           $this->formatPlural(
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.',
180             [
181               '%config_names' => implode(', ', $config_objects),
182               '@extension' => $this->modules['install'][$e->getExtension()]
183             ]),
184           'error'
185         );
186         return;
187       }
188       catch (UnmetDependenciesException $e) {
189         drupal_set_message(
190           $e->getTranslatedMessage($this->getStringTranslation(), $this->modules['install'][$e->getExtension()]),
191           'error'
192         );
193         return;
194       }
195
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),
200       ]));
201     }
202
203     $form_state->setRedirectUrl($this->getCancelUrl());
204   }
205
206 }