3 namespace Drupal\language\Form;
5 use Drupal\Core\Form\ConfigFormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Language\LanguageInterface;
8 use Drupal\Core\Language\LanguageManagerInterface;
9 use Drupal\Core\Config\ConfigFactoryInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
14 * Configure the URL language negotiation method for this site.
16 class NegotiationUrlForm extends ConfigFormBase {
19 * The language manager.
21 * @var \Drupal\Core\Language\LanguageManagerInterface
23 protected $languageManager;
26 * Constructs a new NegotiationUrlForm object.
28 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
29 * The factory for configuration objects.
30 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
31 * The language manager.
33 public function __construct(ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager) {
34 parent::__construct($config_factory);
35 $this->languageManager = $language_manager;
41 public static function create(ContainerInterface $container) {
43 $container->get('config.factory'),
44 $container->get('language_manager')
51 public function getFormId() {
52 return 'language_negotiation_configure_url_form';
58 protected function getEditableConfigNames() {
59 return ['language.negotiation'];
65 public function buildForm(array $form, FormStateInterface $form_state) {
67 $config = $this->config('language.negotiation');
69 $form['language_negotiation_url_part'] = [
70 '#title' => $this->t('Part of the URL that determines language'),
73 LanguageNegotiationUrl::CONFIG_PATH_PREFIX => $this->t('Path prefix'),
74 LanguageNegotiationUrl::CONFIG_DOMAIN => $this->t('Domain'),
76 '#default_value' => $config->get('url.source'),
82 '#title' => $this->t('Path prefix configuration'),
84 '#description' => $this->t('Language codes or other custom text to use as a path prefix for URL language detection. For the selected fallback language, this value may be left blank. <strong>Modifying this value may break existing URLs. Use with caution in a production environment.</strong> Example: Specifying "deutsch" as the path prefix code for German results in URLs like "example.com/deutsch/contact".'),
87 ':input[name="language_negotiation_url_part"]' => [
88 'value' => (string) LanguageNegotiationUrl::CONFIG_PATH_PREFIX,
96 '#title' => $this->t('Domain configuration'),
98 '#description' => $this->t('The domain names to use for these languages. <strong>Modifying this value may break existing URLs. Use with caution in a production environment.</strong> Example: Specifying "de.example.com" as language domain for German will result in a URL like "http://de.example.com/contact".'),
101 ':input[name="language_negotiation_url_part"]' => [
102 'value' => (string) LanguageNegotiationUrl::CONFIG_DOMAIN,
108 $languages = $this->languageManager->getLanguages();
109 $prefixes = $config->get('url.prefixes');
110 $domains = $config->get('url.domains');
111 foreach ($languages as $langcode => $language) {
112 $t_args = ['%language' => $language->getName(), '%langcode' => $language->getId()];
113 $form['prefix'][$langcode] = [
114 '#type' => 'textfield',
115 '#title' => $language->isDefault() ? $this->t('%language (%langcode) path prefix (Default language)', $t_args) : $this->t('%language (%langcode) path prefix', $t_args),
117 '#default_value' => isset($prefixes[$langcode]) ? $prefixes[$langcode] : '',
118 '#field_prefix' => $base_url . '/',
120 $form['domain'][$langcode] = [
121 '#type' => 'textfield',
122 '#title' => $this->t('%language (%langcode) domain', ['%language' => $language->getName(), '%langcode' => $language->getId()]),
124 '#default_value' => isset($domains[$langcode]) ? $domains[$langcode] : '',
128 $form_state->setRedirect('language.negotiation');
130 return parent::buildForm($form, $form_state);
136 public function validateForm(array &$form, FormStateInterface $form_state) {
137 $languages = $this->languageManager->getLanguages();
139 // Count repeated values for uniqueness check.
140 $count = array_count_values($form_state->getValue('prefix'));
141 $default_langcode = $this->config('language.negotiation')->get('selected_langcode');
142 if ($default_langcode == LanguageInterface::LANGCODE_SITE_DEFAULT) {
143 $default_langcode = $this->languageManager->getDefaultLanguage()->getId();
145 foreach ($languages as $langcode => $language) {
146 $value = $form_state->getValue(['prefix', $langcode]);
148 if (!($default_langcode == $langcode) && $form_state->getValue('language_negotiation_url_part') == LanguageNegotiationUrl::CONFIG_PATH_PREFIX) {
149 // Throw a form error if the prefix is blank for a non-default language,
150 // although it is required for selected negotiation type.
151 $form_state->setErrorByName("prefix][$langcode", $this->t('The prefix may only be left blank for the <a href=":url">selected detection fallback language.</a>', [
152 ':url' => $this->getUrlGenerator()->generate('language.negotiation_selected'),
156 elseif (strpos($value, '/') !== FALSE) {
157 // Throw a form error if the string contains a slash,
158 // which would not work.
159 $form_state->setErrorByName("prefix][$langcode", $this->t('The prefix may not contain a slash.'));
161 elseif (isset($count[$value]) && $count[$value] > 1) {
162 // Throw a form error if there are two languages with the same
164 $form_state->setErrorByName("prefix][$langcode", $this->t('The prefix for %language, %value, is not unique.', ['%language' => $language->getName(), '%value' => $value]));
168 // Count repeated values for uniqueness check.
169 $count = array_count_values($form_state->getValue('domain'));
170 foreach ($languages as $langcode => $language) {
171 $value = $form_state->getValue(['domain', $langcode]);
174 if ($form_state->getValue('language_negotiation_url_part') == LanguageNegotiationUrl::CONFIG_DOMAIN) {
175 // Throw a form error if the domain is blank for a non-default language,
176 // although it is required for selected negotiation type.
177 $form_state->setErrorByName("domain][$langcode", $this->t('The domain may not be left blank for %language.', ['%language' => $language->getName()]));
180 elseif (isset($count[$value]) && $count[$value] > 1) {
181 // Throw a form error if there are two languages with the same
183 $form_state->setErrorByName("domain][$langcode", $this->t('The domain for %language, %value, is not unique.', ['%language' => $language->getName(), '%value' => $value]));
187 // Domain names should not contain protocol and/or ports.
188 foreach ($languages as $langcode => $language) {
189 $value = $form_state->getValue(['domain', $langcode]);
190 if (!empty($value)) {
191 // Ensure we have exactly one protocol when checking the hostname.
192 $host = 'http://' . str_replace(['http://', 'https://'], '', $value);
193 if (parse_url($host, PHP_URL_HOST) != $value) {
194 $form_state->setErrorByName("domain][$langcode", $this->t('The domain for %language may only contain the domain name, not a trailing slash, protocol and/or port.', ['%language' => $language->getName()]));
199 parent::validateForm($form, $form_state);
205 public function submitForm(array &$form, FormStateInterface $form_state) {
206 // Save selected format (prefix or domain).
207 $this->config('language.negotiation')
208 ->set('url.source', $form_state->getValue('language_negotiation_url_part'))
209 // Save new domain and prefix values.
210 ->set('url.prefixes', $form_state->getValue('prefix'))
211 ->set('url.domains', $form_state->getValue('domain'))
214 parent::submitForm($form, $form_state);