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.
23 class NegotiationConfigureForm extends ConfigFormBase {
26 * Stores the configuration object for language.types.
28 * @var \Drupal\Core\Config\Config
30 protected $languageTypes;
33 * The language manager.
35 * @var \Drupal\language\ConfigurableLanguageManagerInterface
37 protected $languageManager;
40 * The language negotiator.
42 * @var \Drupal\language\LanguageNegotiatorInterface
44 protected $negotiator;
49 * @var \Drupal\Core\Block\BlockManagerInterface
51 protected $blockManager;
56 * @var \Drupal\Core\Entity\EntityStorageInterface|null
58 protected $blockStorage;
63 * @var \Drupal\Core\Extension\ThemeHandlerInterface
65 protected $themeHandler;
68 * Constructs a NegotiationConfigureForm object.
70 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
71 * The factory for configuration objects.
72 * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
73 * The language manager.
74 * @param \Drupal\language\LanguageNegotiatorInterface $negotiator
75 * The language negotiation methods manager.
76 * @param \Drupal\Core\Block\BlockManagerInterface $block_manager
78 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
80 * @param \Drupal\Core\Entity\EntityStorageInterface $block_storage
81 * The block storage, or NULL if not available.
83 public function __construct(ConfigFactoryInterface $config_factory, ConfigurableLanguageManagerInterface $language_manager, LanguageNegotiatorInterface $negotiator, BlockManagerInterface $block_manager, ThemeHandlerInterface $theme_handler, EntityStorageInterface $block_storage = NULL) {
84 parent::__construct($config_factory);
85 $this->languageTypes = $this->config('language.types');
86 $this->languageManager = $language_manager;
87 $this->negotiator = $negotiator;
88 $this->blockManager = $block_manager;
89 $this->themeHandler = $theme_handler;
90 $this->blockStorage = $block_storage;
96 public static function create(ContainerInterface $container) {
97 $entity_manager = $container->get('entity.manager');
98 $block_storage = $entity_manager->hasHandler('block', 'storage') ? $entity_manager->getStorage('block') : NULL;
100 $container->get('config.factory'),
101 $container->get('language_manager'),
102 $container->get('language_negotiator'),
103 $container->get('plugin.manager.block'),
104 $container->get('theme_handler'),
112 public function getFormId() {
113 return 'language_negotiation_configure_form';
119 protected function getEditableConfigNames() {
120 return ['language.types'];
126 public function buildForm(array $form, FormStateInterface $form_state) {
127 $configurable = $this->languageTypes->get('configurable');
130 '#theme' => 'language_negotiation_configure_form',
131 '#language_types_info' => $this->languageManager->getDefinedLanguageTypesInfo(),
132 '#language_negotiation_info' => $this->negotiator->getNegotiationMethods(),
134 $form['#language_types'] = [];
136 foreach ($form['#language_types_info'] as $type => $info) {
137 // Show locked language types only if they are configurable.
138 if (empty($info['locked']) || in_array($type, $configurable)) {
139 $form['#language_types'][] = $type;
143 foreach ($form['#language_types'] as $type) {
144 $this->configureFormTable($form, $type);
147 $form['actions'] = ['#type' => 'actions'];
148 $form['actions']['submit'] = [
150 '#button_type' => 'primary',
151 '#value' => $this->t('Save settings'),
160 public function submitForm(array &$form, FormStateInterface $form_state) {
161 $configurable_types = $form['#language_types'];
163 $stored_values = $this->languageTypes->get('configurable');
165 $method_weights_type = [];
167 foreach ($configurable_types as $type) {
168 $customized[$type] = in_array($type, $stored_values);
169 $method_weights = [];
170 $enabled_methods = $form_state->getValue([$type, 'enabled']);
171 $enabled_methods[LanguageNegotiationSelected::METHOD_ID] = TRUE;
172 $method_weights_input = $form_state->getValue([$type, 'weight']);
173 if ($form_state->hasValue([$type, 'configurable'])) {
174 $customized[$type] = !$form_state->isValueEmpty([$type, 'configurable']);
177 foreach ($method_weights_input as $method_id => $weight) {
178 if ($enabled_methods[$method_id]) {
179 $method_weights[$method_id] = $weight;
183 $method_weights_type[$type] = $method_weights;
184 $this->languageTypes->set('negotiation.' . $type . '.method_weights', $method_weights_input)->save();
187 // Update non-configurable language types and the related language
188 // negotiation configuration.
189 $this->negotiator->updateConfiguration(array_keys(array_filter($customized)));
191 // Update the language negotiations after setting the configurability.
192 foreach ($method_weights_type as $type => $method_weights) {
193 $this->negotiator->saveConfiguration($type, $method_weights);
196 // Clear block definitions cache since the available blocks and their names
197 // may have been changed based on the configurable types.
198 if ($this->blockStorage) {
199 // If there is an active language switcher for a language type that has
200 // been made not configurable, deactivate it first.
201 $non_configurable = array_keys(array_diff($customized, array_filter($customized)));
202 $this->disableLanguageSwitcher($non_configurable);
204 $this->blockManager->clearCachedDefinitions();
206 $form_state->setRedirect('language.negotiation');
207 drupal_set_message($this->t('Language detection configuration saved.'));
211 * Builds a language negotiation method configuration table.
214 * The language negotiation configuration form.
215 * @param string $type
216 * The language type to generate the table for.
218 protected function configureFormTable(array &$form, $type) {
219 $info = $form['#language_types_info'][$type];
222 '#title' => $this->t('@type language detection', ['@type' => $info['name']]),
224 '#description' => $info['description'],
225 '#language_negotiation_info' => [],
226 '#show_operations' => FALSE,
227 'weight' => ['#tree' => TRUE],
229 // Only show configurability checkbox for the unlocked language types.
230 if (empty($info['locked'])) {
231 $configurable = $this->languageTypes->get('configurable');
232 $table_form['configurable'] = [
233 '#type' => 'checkbox',
234 '#title' => $this->t('Customize %language_name language detection to differ from Interface text language detection settings', ['%language_name' => $info['name']]),
235 '#default_value' => in_array($type, $configurable),
236 '#attributes' => ['class' => ['language-customization-checkbox']],
239 'language/drupal.language.admin'
245 $negotiation_info = $form['#language_negotiation_info'];
246 $enabled_methods = $this->languageTypes->get('negotiation.' . $type . '.enabled') ?: [];
247 $methods_weight = $this->languageTypes->get('negotiation.' . $type . '.method_weights') ?: [];
249 // Add missing data to the methods lists.
250 foreach ($negotiation_info as $method_id => $method) {
251 if (!isset($methods_weight[$method_id])) {
252 $methods_weight[$method_id] = isset($method['weight']) ? $method['weight'] : 0;
256 // Order methods list by weight.
257 asort($methods_weight);
259 foreach ($methods_weight as $method_id => $weight) {
260 // A language method might be no more available if the defining module has
261 // been disabled after the last configuration saving.
262 if (!isset($negotiation_info[$method_id])) {
266 $enabled = isset($enabled_methods[$method_id]);
267 $method = $negotiation_info[$method_id];
269 // List the method only if the current type is defined in its 'types' key.
270 // If it is not defined default to all the configurable language types.
271 $types = array_flip(isset($method['types']) ? $method['types'] : $form['#language_types']);
273 if (isset($types[$type])) {
274 $table_form['#language_negotiation_info'][$method_id] = $method;
275 $method_name = $method['name'];
277 $table_form['weight'][$method_id] = [
279 '#title' => $this->t('Weight for @title language detection method', ['@title' => Unicode::strtolower($method_name)]),
280 '#title_display' => 'invisible',
281 '#default_value' => $weight,
282 '#attributes' => ['class' => ["language-method-weight-$type"]],
286 $table_form['title'][$method_id] = ['#plain_text' => $method_name];
288 $table_form['enabled'][$method_id] = [
289 '#type' => 'checkbox',
290 '#title' => $this->t('Enable @title language detection method', ['@title' => Unicode::strtolower($method_name)]),
291 '#title_display' => 'invisible',
292 '#default_value' => $enabled,
294 if ($method_id === LanguageNegotiationSelected::METHOD_ID) {
295 $table_form['enabled'][$method_id]['#default_value'] = TRUE;
296 $table_form['enabled'][$method_id]['#attributes'] = ['disabled' => 'disabled'];
299 $table_form['description'][$method_id] = ['#markup' => $method['description']];
302 if (isset($method['config_route_name'])) {
303 $config_op['configure'] = [
304 'title' => $this->t('Configure'),
305 'url' => Url::fromRoute($method['config_route_name']),
307 // If there is at least one operation enabled show the operation
309 $table_form['#show_operations'] = TRUE;
311 $table_form['operation'][$method_id] = [
312 '#type' => 'operations',
313 '#links' => $config_op,
317 $form[$type] = $table_form;
321 * Disables the language switcher blocks.
323 * @param array $language_types
324 * An array containing all language types whose language switchers need to
327 protected function disableLanguageSwitcher(array $language_types) {
328 $theme = $this->themeHandler->getDefault();
329 $blocks = $this->blockStorage->loadByProperties(['theme' => $theme]);
330 foreach ($language_types as $language_type) {
331 foreach ($blocks as $block) {
332 if ($block->getPluginId() == 'language_block:' . $language_type) {