View source
<?php
namespace Drupal\language\Form;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\language\LanguageNegotiatorInterface;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSelected;
use Symfony\Component\DependencyInjection\ContainerInterface;
class NegotiationConfigureForm extends ConfigFormBase {
protected $languageTypes;
protected $languageManager;
protected $negotiator;
protected $blockManager;
protected $blockStorage;
protected $themeHandler;
public function __construct(ConfigFactoryInterface $config_factory, ConfigurableLanguageManagerInterface $language_manager, LanguageNegotiatorInterface $negotiator, BlockManagerInterface $block_manager, ThemeHandlerInterface $theme_handler, EntityStorageInterface $block_storage = NULL) {
parent::__construct($config_factory);
$this->languageTypes = $this
->config('language.types');
$this->languageManager = $language_manager;
$this->negotiator = $negotiator;
$this->blockManager = $block_manager;
$this->themeHandler = $theme_handler;
$this->blockStorage = $block_storage;
}
public static function create(ContainerInterface $container) {
$entity_manager = $container
->get('entity.manager');
$block_storage = $entity_manager
->hasHandler('block', 'storage') ? $entity_manager
->getStorage('block') : NULL;
return new static($container
->get('config.factory'), $container
->get('language_manager'), $container
->get('language_negotiator'), $container
->get('plugin.manager.block'), $container
->get('theme_handler'), $block_storage);
}
public function getFormId() {
return 'language_negotiation_configure_form';
}
protected function getEditableConfigNames() {
return [
'language.types',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$configurable = $this->languageTypes
->get('configurable');
$form = array(
'#theme' => 'language_negotiation_configure_form',
'#language_types_info' => $this->languageManager
->getDefinedLanguageTypesInfo(),
'#language_negotiation_info' => $this->negotiator
->getNegotiationMethods(),
);
$form['#language_types'] = array();
foreach ($form['#language_types_info'] as $type => $info) {
if (empty($info['locked']) || in_array($type, $configurable)) {
$form['#language_types'][] = $type;
}
}
foreach ($form['#language_types'] as $type) {
$this
->configureFormTable($form, $type);
}
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#button_type' => 'primary',
'#value' => $this
->t('Save settings'),
);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$configurable_types = $form['#language_types'];
$stored_values = $this->languageTypes
->get('configurable');
$customized = array();
$method_weights_type = array();
foreach ($configurable_types as $type) {
$customized[$type] = in_array($type, $stored_values);
$method_weights = array();
$enabled_methods = $form_state
->getValue(array(
$type,
'enabled',
));
$enabled_methods[LanguageNegotiationSelected::METHOD_ID] = TRUE;
$method_weights_input = $form_state
->getValue(array(
$type,
'weight',
));
if ($form_state
->hasValue(array(
$type,
'configurable',
))) {
$customized[$type] = !$form_state
->isValueEmpty(array(
$type,
'configurable',
));
}
foreach ($method_weights_input as $method_id => $weight) {
if ($enabled_methods[$method_id]) {
$method_weights[$method_id] = $weight;
}
}
$method_weights_type[$type] = $method_weights;
$this->languageTypes
->set('negotiation.' . $type . '.method_weights', $method_weights_input)
->save();
}
$this->negotiator
->updateConfiguration(array_keys(array_filter($customized)));
foreach ($method_weights_type as $type => $method_weights) {
$this->negotiator
->saveConfiguration($type, $method_weights);
}
if ($this->blockStorage) {
$non_configurable = array_keys(array_diff($customized, array_filter($customized)));
$this
->disableLanguageSwitcher($non_configurable);
}
$this->blockManager
->clearCachedDefinitions();
$form_state
->setRedirect('language.negotiation');
drupal_set_message($this
->t('Language detection configuration saved.'));
}
protected function configureFormTable(array &$form, $type) {
$info = $form['#language_types_info'][$type];
$table_form = array(
'#title' => $this
->t('@type language detection', array(
'@type' => $info['name'],
)),
'#tree' => TRUE,
'#description' => $info['description'],
'#language_negotiation_info' => array(),
'#show_operations' => FALSE,
'weight' => array(
'#tree' => TRUE,
),
);
if (empty($info['locked'])) {
$configurable = $this->languageTypes
->get('configurable');
$table_form['configurable'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Customize %language_name language detection to differ from Interface text language detection settings', array(
'%language_name' => $info['name'],
)),
'#default_value' => in_array($type, $configurable),
'#attributes' => array(
'class' => array(
'language-customization-checkbox',
),
),
'#attached' => array(
'library' => array(
'language/drupal.language.admin',
),
),
);
}
$negotiation_info = $form['#language_negotiation_info'];
$enabled_methods = $this->languageTypes
->get('negotiation.' . $type . '.enabled') ?: array();
$methods_weight = $this->languageTypes
->get('negotiation.' . $type . '.method_weights') ?: array();
foreach ($negotiation_info as $method_id => $method) {
if (!isset($methods_weight[$method_id])) {
$methods_weight[$method_id] = isset($method['weight']) ? $method['weight'] : 0;
}
}
asort($methods_weight);
foreach ($methods_weight as $method_id => $weight) {
if (!isset($negotiation_info[$method_id])) {
continue;
}
$enabled = isset($enabled_methods[$method_id]);
$method = $negotiation_info[$method_id];
$types = array_flip(isset($method['types']) ? $method['types'] : $form['#language_types']);
if (isset($types[$type])) {
$table_form['#language_negotiation_info'][$method_id] = $method;
$method_name = $method['name'];
$table_form['weight'][$method_id] = array(
'#type' => 'weight',
'#title' => $this
->t('Weight for @title language detection method', array(
'@title' => Unicode::strtolower($method_name),
)),
'#title_display' => 'invisible',
'#default_value' => $weight,
'#attributes' => array(
'class' => array(
"language-method-weight-{$type}",
),
),
'#delta' => 20,
);
$table_form['title'][$method_id] = array(
'#plain_text' => $method_name,
);
$table_form['enabled'][$method_id] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Enable @title language detection method', array(
'@title' => Unicode::strtolower($method_name),
)),
'#title_display' => 'invisible',
'#default_value' => $enabled,
);
if ($method_id === LanguageNegotiationSelected::METHOD_ID) {
$table_form['enabled'][$method_id]['#default_value'] = TRUE;
$table_form['enabled'][$method_id]['#attributes'] = array(
'disabled' => 'disabled',
);
}
$table_form['description'][$method_id] = array(
'#markup' => $method['description'],
);
$config_op = array();
if (isset($method['config_route_name'])) {
$config_op['configure'] = array(
'title' => $this
->t('Configure'),
'url' => Url::fromRoute($method['config_route_name']),
);
$table_form['#show_operations'] = TRUE;
}
$table_form['operation'][$method_id] = array(
'#type' => 'operations',
'#links' => $config_op,
);
}
}
$form[$type] = $table_form;
}
protected function disableLanguageSwitcher(array $language_types) {
$theme = $this->themeHandler
->getDefault();
$blocks = $this->blockStorage
->loadByProperties(array(
'theme' => $theme,
));
foreach ($language_types as $language_type) {
foreach ($blocks as $block) {
if ($block
->getPluginId() == 'language_block:' . $language_type) {
$block
->delete();
}
}
}
}
}