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