Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / language / src / Form / NegotiationConfigureForm.php
1 <?php
2
3 namespace Drupal\language\Form;
4
5 use Drupal\Core\Block\BlockManagerInterface;
6 use Drupal\Component\Utility\Unicode;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\Core\Extension\ThemeHandlerInterface;
10 use Drupal\Core\Form\ConfigFormBase;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\Core\Url;
13 use Drupal\language\ConfigurableLanguageManagerInterface;
14 use Drupal\language\LanguageNegotiatorInterface;
15 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSelected;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17
18 /**
19  * Configure the selected language negotiation method for this site.
20  */
21 class NegotiationConfigureForm extends ConfigFormBase {
22
23   /**
24    * Stores the configuration object for language.types.
25    *
26    * @var \Drupal\Core\Config\Config
27    */
28   protected $languageTypes;
29
30   /**
31    * The language manager.
32    *
33    * @var \Drupal\language\ConfigurableLanguageManagerInterface
34    */
35   protected $languageManager;
36
37   /**
38    * The language negotiator.
39    *
40    * @var \Drupal\language\LanguageNegotiatorInterface
41    */
42   protected $negotiator;
43
44   /**
45    * The block manager.
46    *
47    * @var \Drupal\Core\Block\BlockManagerInterface
48    */
49   protected $blockManager;
50
51   /**
52    * The block storage.
53    *
54    * @var \Drupal\Core\Entity\EntityStorageInterface|null
55    */
56   protected $blockStorage;
57
58   /**
59    * The theme handler.
60    *
61    * @var \Drupal\Core\Extension\ThemeHandlerInterface
62    */
63   protected $themeHandler;
64
65   /**
66    * Constructs a NegotiationConfigureForm object.
67    *
68    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
69    *   The factory for configuration objects.
70    * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
71    *   The language manager.
72    * @param \Drupal\language\LanguageNegotiatorInterface $negotiator
73    *   The language negotiation methods manager.
74    * @param \Drupal\Core\Block\BlockManagerInterface $block_manager
75    *   The block manager.
76    * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
77    *   The theme handler.
78    * @param \Drupal\Core\Entity\EntityStorageInterface $block_storage
79    *   The block storage, or NULL if not available.
80    */
81   public function __construct(ConfigFactoryInterface $config_factory, ConfigurableLanguageManagerInterface $language_manager, LanguageNegotiatorInterface $negotiator, BlockManagerInterface $block_manager, ThemeHandlerInterface $theme_handler, EntityStorageInterface $block_storage = NULL) {
82     parent::__construct($config_factory);
83     $this->languageTypes = $this->config('language.types');
84     $this->languageManager = $language_manager;
85     $this->negotiator = $negotiator;
86     $this->blockManager = $block_manager;
87     $this->themeHandler = $theme_handler;
88     $this->blockStorage = $block_storage;
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public static function create(ContainerInterface $container) {
95     $entity_manager = $container->get('entity.manager');
96     $block_storage = $entity_manager->hasHandler('block', 'storage') ? $entity_manager->getStorage('block') : NULL;
97     return new static(
98       $container->get('config.factory'),
99       $container->get('language_manager'),
100       $container->get('language_negotiator'),
101       $container->get('plugin.manager.block'),
102       $container->get('theme_handler'),
103       $block_storage
104     );
105   }
106
107   /**
108    * {@inheritdoc}
109    */
110   public function getFormId() {
111     return 'language_negotiation_configure_form';
112   }
113
114   /**
115    * {@inheritdoc}
116    */
117   protected function getEditableConfigNames() {
118     return ['language.types'];
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   public function buildForm(array $form, FormStateInterface $form_state) {
125     $configurable = $this->languageTypes->get('configurable');
126
127     $form = [
128       '#theme' => 'language_negotiation_configure_form',
129       '#language_types_info' => $this->languageManager->getDefinedLanguageTypesInfo(),
130       '#language_negotiation_info' => $this->negotiator->getNegotiationMethods(),
131     ];
132     $form['#language_types'] = [];
133
134     foreach ($form['#language_types_info'] as $type => $info) {
135       // Show locked language types only if they are configurable.
136       if (empty($info['locked']) || in_array($type, $configurable)) {
137         $form['#language_types'][] = $type;
138       }
139     }
140
141     foreach ($form['#language_types'] as $type) {
142       $this->configureFormTable($form, $type);
143     }
144
145     $form['actions'] = ['#type' => 'actions'];
146     $form['actions']['submit'] = [
147       '#type' => 'submit',
148       '#button_type' => 'primary',
149       '#value' => $this->t('Save settings'),
150     ];
151
152     return $form;
153   }
154
155   /**
156    * {@inheritdoc}
157    */
158   public function submitForm(array &$form, FormStateInterface $form_state) {
159     $configurable_types = $form['#language_types'];
160
161     $stored_values = $this->languageTypes->get('configurable');
162     $customized = [];
163     $method_weights_type = [];
164
165     foreach ($configurable_types as $type) {
166       $customized[$type] = in_array($type, $stored_values);
167       $method_weights = [];
168       $enabled_methods = $form_state->getValue([$type, 'enabled']);
169       $enabled_methods[LanguageNegotiationSelected::METHOD_ID] = TRUE;
170       $method_weights_input = $form_state->getValue([$type, 'weight']);
171       if ($form_state->hasValue([$type, 'configurable'])) {
172         $customized[$type] = !$form_state->isValueEmpty([$type, 'configurable']);
173       }
174
175       foreach ($method_weights_input as $method_id => $weight) {
176         if ($enabled_methods[$method_id]) {
177           $method_weights[$method_id] = $weight;
178         }
179       }
180
181       $method_weights_type[$type] = $method_weights;
182       $this->languageTypes->set('negotiation.' . $type . '.method_weights', $method_weights_input)->save();
183     }
184
185     // Update non-configurable language types and the related language
186     // negotiation configuration.
187     $this->negotiator->updateConfiguration(array_keys(array_filter($customized)));
188
189     // Update the language negotiations after setting the configurability.
190     foreach ($method_weights_type as $type => $method_weights) {
191       $this->negotiator->saveConfiguration($type, $method_weights);
192     }
193
194     // Clear block definitions cache since the available blocks and their names
195     // may have been changed based on the configurable types.
196     if ($this->blockStorage) {
197       // If there is an active language switcher for a language type that has
198       // been made not configurable, deactivate it first.
199       $non_configurable = array_keys(array_diff($customized, array_filter($customized)));
200       $this->disableLanguageSwitcher($non_configurable);
201     }
202     $this->blockManager->clearCachedDefinitions();
203
204     $form_state->setRedirect('language.negotiation');
205     drupal_set_message($this->t('Language detection configuration saved.'));
206   }
207
208   /**
209    * Builds a language negotiation method configuration table.
210    *
211    * @param array $form
212    *   The language negotiation configuration form.
213    * @param string $type
214    *   The language type to generate the table for.
215    */
216   protected function configureFormTable(array &$form, $type) {
217     $info = $form['#language_types_info'][$type];
218
219     $table_form = [
220       '#title' => $this->t('@type language detection', ['@type' => $info['name']]),
221       '#tree' => TRUE,
222       '#description' => $info['description'],
223       '#language_negotiation_info' => [],
224       '#show_operations' => FALSE,
225       'weight' => ['#tree' => TRUE],
226     ];
227     // Only show configurability checkbox for the unlocked language types.
228     if (empty($info['locked'])) {
229       $configurable = $this->languageTypes->get('configurable');
230       $table_form['configurable'] = [
231         '#type' => 'checkbox',
232         '#title' => $this->t('Customize %language_name language detection to differ from Interface text language detection settings', ['%language_name' => $info['name']]),
233         '#default_value' => in_array($type, $configurable),
234         '#attributes' => ['class' => ['language-customization-checkbox']],
235         '#attached' => [
236           'library' => [
237             'language/drupal.language.admin'
238           ],
239         ],
240       ];
241     }
242
243     $negotiation_info = $form['#language_negotiation_info'];
244     $enabled_methods = $this->languageTypes->get('negotiation.' . $type . '.enabled') ?: [];
245     $methods_weight = $this->languageTypes->get('negotiation.' . $type . '.method_weights') ?: [];
246
247     // Add missing data to the methods lists.
248     foreach ($negotiation_info as $method_id => $method) {
249       if (!isset($methods_weight[$method_id])) {
250         $methods_weight[$method_id] = isset($method['weight']) ? $method['weight'] : 0;
251       }
252     }
253
254     // Order methods list by weight.
255     asort($methods_weight);
256
257     foreach ($methods_weight as $method_id => $weight) {
258       // A language method might be no more available if the defining module has
259       // been disabled after the last configuration saving.
260       if (!isset($negotiation_info[$method_id])) {
261         continue;
262       }
263
264       $enabled = isset($enabled_methods[$method_id]);
265       $method = $negotiation_info[$method_id];
266
267       // List the method only if the current type is defined in its 'types' key.
268       // If it is not defined default to all the configurable language types.
269       $types = array_flip(isset($method['types']) ? $method['types'] : $form['#language_types']);
270
271       if (isset($types[$type])) {
272         $table_form['#language_negotiation_info'][$method_id] = $method;
273         $method_name = $method['name'];
274
275         $table_form['weight'][$method_id] = [
276           '#type' => 'weight',
277           '#title' => $this->t('Weight for @title language detection method', ['@title' => Unicode::strtolower($method_name)]),
278           '#title_display' => 'invisible',
279           '#default_value' => $weight,
280           '#attributes' => ['class' => ["language-method-weight-$type"]],
281           '#delta' => 20,
282         ];
283
284         $table_form['title'][$method_id] = ['#plain_text' => $method_name];
285
286         $table_form['enabled'][$method_id] = [
287           '#type' => 'checkbox',
288           '#title' => $this->t('Enable @title language detection method', ['@title' => Unicode::strtolower($method_name)]),
289           '#title_display' => 'invisible',
290           '#default_value' => $enabled,
291         ];
292         if ($method_id === LanguageNegotiationSelected::METHOD_ID) {
293           $table_form['enabled'][$method_id]['#default_value'] = TRUE;
294           $table_form['enabled'][$method_id]['#attributes'] = ['disabled' => 'disabled'];
295         }
296
297         $table_form['description'][$method_id] = ['#markup' => $method['description']];
298
299         $config_op = [];
300         if (isset($method['config_route_name'])) {
301           $config_op['configure'] = [
302             'title' => $this->t('Configure'),
303             'url' => Url::fromRoute($method['config_route_name']),
304           ];
305           // If there is at least one operation enabled show the operation
306           // column.
307           $table_form['#show_operations'] = TRUE;
308         }
309         $table_form['operation'][$method_id] = [
310          '#type' => 'operations',
311          '#links' => $config_op,
312         ];
313       }
314     }
315     $form[$type] = $table_form;
316   }
317
318   /**
319    * Disables the language switcher blocks.
320    *
321    * @param array $language_types
322    *   An array containing all language types whose language switchers need to
323    *   be disabled.
324    */
325   protected function disableLanguageSwitcher(array $language_types) {
326     $theme = $this->themeHandler->getDefault();
327     $blocks = $this->blockStorage->loadByProperties(['theme' => $theme]);
328     foreach ($language_types as $language_type) {
329       foreach ($blocks as $block) {
330         if ($block->getPluginId() == 'language_block:' . $language_type) {
331           $block->delete();
332         }
333       }
334     }
335   }
336
337 }