3 namespace Drupal\language\Form;
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;
13 use Drupal\language\ConfigurableLanguageManagerInterface;
14 use Drupal\language\LanguageNegotiatorInterface;
15 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSelected;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
19 * Configure the selected language negotiation method for this site.
21 class NegotiationConfigureForm extends ConfigFormBase {
24 * Stores the configuration object for language.types.
26 * @var \Drupal\Core\Config\Config
28 protected $languageTypes;
31 * The language manager.
33 * @var \Drupal\language\ConfigurableLanguageManagerInterface
35 protected $languageManager;
38 * The language negotiator.
40 * @var \Drupal\language\LanguageNegotiatorInterface
42 protected $negotiator;
47 * @var \Drupal\Core\Block\BlockManagerInterface
49 protected $blockManager;
54 * @var \Drupal\Core\Entity\EntityStorageInterface|null
56 protected $blockStorage;
61 * @var \Drupal\Core\Extension\ThemeHandlerInterface
63 protected $themeHandler;
66 * Constructs a NegotiationConfigureForm object.
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
76 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
78 * @param \Drupal\Core\Entity\EntityStorageInterface $block_storage
79 * The block storage, or NULL if not available.
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;
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;
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'),
110 public function getFormId() {
111 return 'language_negotiation_configure_form';
117 protected function getEditableConfigNames() {
118 return ['language.types'];
124 public function buildForm(array $form, FormStateInterface $form_state) {
125 $configurable = $this->languageTypes->get('configurable');
128 '#theme' => 'language_negotiation_configure_form',
129 '#language_types_info' => $this->languageManager->getDefinedLanguageTypesInfo(),
130 '#language_negotiation_info' => $this->negotiator->getNegotiationMethods(),
132 $form['#language_types'] = [];
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;
141 foreach ($form['#language_types'] as $type) {
142 $this->configureFormTable($form, $type);
145 $form['actions'] = ['#type' => 'actions'];
146 $form['actions']['submit'] = [
148 '#button_type' => 'primary',
149 '#value' => $this->t('Save settings'),
158 public function submitForm(array &$form, FormStateInterface $form_state) {
159 $configurable_types = $form['#language_types'];
161 $stored_values = $this->languageTypes->get('configurable');
163 $method_weights_type = [];
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']);
175 foreach ($method_weights_input as $method_id => $weight) {
176 if ($enabled_methods[$method_id]) {
177 $method_weights[$method_id] = $weight;
181 $method_weights_type[$type] = $method_weights;
182 $this->languageTypes->set('negotiation.' . $type . '.method_weights', $method_weights_input)->save();
185 // Update non-configurable language types and the related language
186 // negotiation configuration.
187 $this->negotiator->updateConfiguration(array_keys(array_filter($customized)));
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);
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);
202 $this->blockManager->clearCachedDefinitions();
204 $form_state->setRedirect('language.negotiation');
205 drupal_set_message($this->t('Language detection configuration saved.'));
209 * Builds a language negotiation method configuration table.
212 * The language negotiation configuration form.
213 * @param string $type
214 * The language type to generate the table for.
216 protected function configureFormTable(array &$form, $type) {
217 $info = $form['#language_types_info'][$type];
220 '#title' => $this->t('@type language detection', ['@type' => $info['name']]),
222 '#description' => $info['description'],
223 '#language_negotiation_info' => [],
224 '#show_operations' => FALSE,
225 'weight' => ['#tree' => TRUE],
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']],
237 'language/drupal.language.admin'
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') ?: [];
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;
254 // Order methods list by weight.
255 asort($methods_weight);
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])) {
264 $enabled = isset($enabled_methods[$method_id]);
265 $method = $negotiation_info[$method_id];
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']);
271 if (isset($types[$type])) {
272 $table_form['#language_negotiation_info'][$method_id] = $method;
273 $method_name = $method['name'];
275 $table_form['weight'][$method_id] = [
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"]],
284 $table_form['title'][$method_id] = ['#plain_text' => $method_name];
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,
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'];
297 $table_form['description'][$method_id] = ['#markup' => $method['description']];
300 if (isset($method['config_route_name'])) {
301 $config_op['configure'] = [
302 'title' => $this->t('Configure'),
303 'url' => Url::fromRoute($method['config_route_name']),
305 // If there is at least one operation enabled show the operation
307 $table_form['#show_operations'] = TRUE;
309 $table_form['operation'][$method_id] = [
310 '#type' => 'operations',
311 '#links' => $config_op,
315 $form[$type] = $table_form;
319 * Disables the language switcher blocks.
321 * @param array $language_types
322 * An array containing all language types whose language switchers need to
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) {