3 namespace Drupal\language\Form;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Form\ConfigFormBase;
7 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\language\ConfigurableLanguageManagerInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Configure the browser language negotiation method for this site.
17 class NegotiationBrowserForm extends ConfigFormBase {
20 * The configurable language manager.
22 * @var \Drupal\language\ConfigurableLanguageManagerInterface
24 protected $languageManager;
29 public function __construct(ConfigFactoryInterface $config_factory, ConfigurableLanguageManagerInterface $language_manager) {
30 parent::__construct($config_factory);
31 $this->languageManager = $language_manager;
37 public static function create(ContainerInterface $container) {
39 $container->get('config.factory'),
40 $container->get('language_manager')
47 public function getFormId() {
48 return 'language_negotiation_configure_browser_form';
54 protected function getEditableConfigNames() {
55 return ['language.mappings'];
61 public function buildForm(array $form, FormStateInterface $form_state) {
64 // Initialize a language list to the ones available, including English.
65 $languages = $this->languageManager->getLanguages();
67 $existing_languages = [];
68 foreach ($languages as $langcode => $language) {
69 $existing_languages[$langcode] = $language->getName();
72 // If we have no languages available, present the list of predefined languages
73 // only. If we do have already added languages, set up two option groups with
74 // the list of existing and then predefined languages.
75 if (empty($existing_languages)) {
76 $language_options = $this->languageManager->getStandardLanguageListWithoutConfigured();
80 (string) $this->t('Existing languages') => $existing_languages,
81 (string) $this->t('Languages not yet added') => $this->languageManager->getStandardLanguageListWithoutConfigured(),
88 $this->t('Browser language code'),
89 $this->t('Site language'),
90 $this->t('Operations'),
92 '#attributes' => ['id' => 'language-negotiation-browser'],
93 '#empty' => $this->t('No browser language mappings available.'),
96 $mappings = $this->language_get_browser_drupal_langcode_mappings();
97 foreach ($mappings as $browser_langcode => $drupal_langcode) {
98 $form['mappings'][$browser_langcode] = [
99 'browser_langcode' => [
100 '#title' => $this->t('Browser language code'),
101 '#title_display' => 'invisible',
102 '#type' => 'textfield',
103 '#default_value' => $browser_langcode,
107 'drupal_langcode' => [
108 '#title' => $this->t('Site language'),
109 '#title_display' => 'invisible',
111 '#options' => $language_options,
112 '#default_value' => $drupal_langcode,
116 // Operations column.
117 $form['mappings'][$browser_langcode]['operations'] = [
118 '#type' => 'operations',
121 $form['mappings'][$browser_langcode]['operations']['#links']['delete'] = [
122 'title' => $this->t('Delete'),
123 'url' => Url::fromRoute('language.negotiation_browser_delete', ['browser_langcode' => $browser_langcode]),
128 $form['new_mapping'] = [
129 '#type' => 'details',
130 '#title' => $this->t('Add a new mapping'),
133 $form['new_mapping']['browser_langcode'] = [
134 '#type' => 'textfield',
135 '#title' => $this->t('Browser language code'),
136 '#description' => $this->t('Use language codes as <a href=":w3ctags">defined by the W3C</a> for interoperability. <em>Examples: "en", "en-gb" and "zh-hant".</em>', [':w3ctags' => 'http://www.w3.org/International/articles/language-tags/']),
139 $form['new_mapping']['drupal_langcode'] = [
141 '#title' => $this->t('Site language'),
142 '#options' => $language_options,
145 return parent::buildForm($form, $form_state);
151 public function validateForm(array &$form, FormStateInterface $form_state) {
152 // Array to check if all browser language codes are unique.
155 // Check all mappings.
156 if ($form_state->hasValue('mappings')) {
157 $mappings = $form_state->getValue('mappings');
158 foreach ($mappings as $key => $data) {
159 // Make sure browser_langcode is unique.
160 if (array_key_exists($data['browser_langcode'], $unique_values)) {
161 $form_state->setErrorByName('mappings][new_mapping][browser_langcode', $this->t('Browser language codes must be unique.'));
163 elseif (preg_match('/[^a-z\-]/', $data['browser_langcode'])) {
164 $form_state->setErrorByName('mappings][new_mapping][browser_langcode', $this->t('Browser language codes can only contain lowercase letters and a hyphen(-).'));
166 $unique_values[$data['browser_langcode']] = $data['drupal_langcode'];
170 // Check new mapping.
171 $data = $form_state->getValue('new_mapping');
172 if (!empty($data['browser_langcode'])) {
173 // Make sure browser_langcode is unique.
174 if (array_key_exists($data['browser_langcode'], $unique_values)) {
175 $form_state->setErrorByName('mappings][' . $key . '][browser_langcode', $this->t('Browser language codes must be unique.'));
177 elseif (preg_match('/[^a-z\-]/', $data['browser_langcode'])) {
178 $form_state->setErrorByName('mappings][' . $key . '][browser_langcode', $this->t('Browser language codes can only contain lowercase letters and a hyphen(-).'));
180 $unique_values[$data['browser_langcode']] = $data['drupal_langcode'];
183 $form_state->set('mappings', $unique_values);
189 public function submitForm(array &$form, FormStateInterface $form_state) {
190 $mappings = $form_state->get('mappings');
191 if (!empty($mappings)) {
192 $config = $this->config('language.mappings');
193 $config->setData(['map' => $mappings]);
197 parent::submitForm($form, $form_state);
201 * Retrieves the browser's langcode mapping configuration array.
204 * The browser's langcode mapping configuration array.
206 protected function language_get_browser_drupal_langcode_mappings() {
207 $config = $this->config('language.mappings');
208 if ($config->isNew()) {
211 return $config->get('map');