Commit 7d01643e authored by alexpott's avatar alexpott

Issue #2023091 by djevans, badrange: Drupal does not allow W3C compliant...

Issue #2023091 by djevans, badrange: Drupal does not allow W3C compliant language codes where you target a numeric region
parent c9b8b838
......@@ -100,8 +100,11 @@ public function commonForm(array &$form) {
*/
public function validateCommon(array $form, FormStateInterface $form_state) {
// Ensure sane field values for langcode and name.
if (!isset($form['langcode_view']) && preg_match('@[^a-zA-Z_-]@', $form_state->getValue('langcode'))) {
$form_state->setErrorByName('langcode', $this->t('%field may only contain characters a-z, underscores, or hyphens.', array('%field' => $form['langcode']['#title'])));
if (!isset($form['langcode_view']) && !preg_match('@^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$@', $form_state->getValue('langcode'))) {
$form_state->setErrorByName('langcode', $this->t('%field must be a valid language tag as <a href="@url">defined by the W3C</a>.', array(
'%field' => $form['langcode']['#title'],
'@url' => 'http://www.w3.org/International/articles/language-tags/',
)));
}
if ($form_state->getValue('label') != String::checkPlain($form_state->getValue('label'))) {
$form_state->setErrorByName('label', $this->t('%field cannot contain any markup.', array('%field' => $form['label']['#title'])));
......
......@@ -54,10 +54,30 @@ public function testLanguageConfiguration() {
'direction' => LanguageInterface::DIRECTION_LTR,
);
$this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
$this->assertRaw(t('%field may only contain characters a-z, underscores, or hyphens.', array('%field' => t('Language code'))));
$this->assertRaw(t('%field must be a valid language tag as <a href="@url">defined by the W3C</a>.', array(
'%field' => t('Language code'),
'@url' => 'http://www.w3.org/International/articles/language-tags/',
)));
$this->assertRaw(t('%field cannot contain any markup.', array('%field' => t('Language name in English'))));
$this->assertUrl(\Drupal::url('language.add', array(), array('absolute' => TRUE)), [], 'Correct page redirection.');
// Test adding a custom language with a numeric region code.
$edit = array(
'predefined_langcode' => 'custom',
'langcode' => 'es-419',
'label' => 'Latin American Spanish',
'direction' => LanguageInterface::DIRECTION_LTR,
);
$this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
$this->assertRaw(t(
'The language %language has been created and can now be used.',
array('%language' => $edit['label'])
));
$this->assertUrl(\Drupal::url('language.admin_overview', array(), array('absolute' => TRUE)), [], 'Correct page redirection.');
// Test validation of existing language values.
$edit = array(
'predefined_langcode' => 'custom',
......
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