Commit a0add300 authored by alexpott's avatar alexpott

Issue #2411343 by pcambra: Only allowing to have empty language path prefix...

Issue #2411343 by pcambra: Only allowing to have empty language path prefix for the default language is inappropriate
parent e25c440c
......@@ -9,6 +9,7 @@
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -85,7 +86,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#tree' => TRUE,
'#title' => $this->t('Path prefix configuration'),
'#open' => TRUE,
'#description' => $this->t('Language codes or other custom text to use as a path prefix for URL language detection. For the default 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".'),
'#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".'),
'#states' => array(
'visible' => array(
':input[name="language_negotiation_url_part"]' => array(
......@@ -142,14 +143,17 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
// Count repeated values for uniqueness check.
$count = array_count_values($form_state->getValue('prefix'));
$default_langcode = $this->config('language.negotiation')->get('selected_langcode');
if ($default_langcode == LanguageInterface::LANGCODE_SITE_DEFAULT) {
$default_langcode = $this->languageManager->getDefaultLanguage()->getId();
}
foreach ($languages as $langcode => $language) {
$value = $form_state->getValue(array('prefix', $langcode));
if ($value === '') {
if (!$language->isDefault() && $form_state->getValue('language_negotiation_url_part') == LanguageNegotiationUrl::CONFIG_PATH_PREFIX) {
if (!($default_langcode == $langcode) && $form_state->getValue('language_negotiation_url_part') == LanguageNegotiationUrl::CONFIG_PATH_PREFIX) {
// Throw a form error if the prefix is blank for a non-default language,
// although it is required for selected negotiation type.
$form_state->setErrorByName("prefix][$langcode", $this->t('The prefix may only be left blank for the default language.'));
$form_state->setErrorByName("prefix][$langcode", $this->t('The prefix may only be left blank for the selected negotiation fallback language.'));
}
}
elseif (strpos($value, '/') !== FALSE) {
......
......@@ -83,13 +83,16 @@ function testLanguageConfiguration() {
$this->drupalPostForm(NULL, $edit, t('Save configuration'));
$this->assertFieldByXPath('//input[@name="prefix[fr]"]', 'french', 'French path prefix has changed.');
// Check that prefix of non default language cannot be changed to
// empty string.
// Change default negotiation language.
$this->config('language.negotiation')->set('selected_langcode', 'fr')->save();
// Check that the prefix of a language that is not the negotiation one
// cannot be changed to empty string.
$edit = array(
'prefix[en]' => '',
);
$this->drupalPostForm(NULL, $edit, t('Save configuration'));
$this->assertText(t('The prefix may only be left blank for the default language.'), 'English prefix cannot be changed to empty string.');
$this->assertText(t('The prefix may only be left blank for the selected negotiation fallback language.'));
// Check that prefix cannot be changed to contain a slash.
$edit = array(
......
......@@ -161,7 +161,7 @@ protected function doTestLanguageBlockAnonymous($block_label) {
}
/**
* Test languge switcher links for domain based negotiation
* Test language switcher links for domain based negotiation.
*/
function testLanguageBlockWithDomain() {
// Add the Italian language.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment