function NegotiationBrowserForm::validateForm

Same name in other branches
  1. 9 core/modules/language/src/Form/NegotiationBrowserForm.php \Drupal\language\Form\NegotiationBrowserForm::validateForm()
  2. 10 core/modules/language/src/Form/NegotiationBrowserForm.php \Drupal\language\Form\NegotiationBrowserForm::validateForm()
  3. 11.x core/modules/language/src/Form/NegotiationBrowserForm.php \Drupal\language\Form\NegotiationBrowserForm::validateForm()

Overrides FormBase::validateForm

File

core/modules/language/src/Form/NegotiationBrowserForm.php, line 151

Class

NegotiationBrowserForm
Configure the browser language negotiation method for this site.

Namespace

Drupal\language\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
    // Array to check if all browser language codes are unique.
    $unique_values = [];
    // Check all mappings.
    if ($form_state->hasValue('mappings')) {
        $mappings = $form_state->getValue('mappings');
        foreach ($mappings as $key => $data) {
            // Make sure browser_langcode is unique.
            if (array_key_exists($data['browser_langcode'], $unique_values)) {
                $form_state->setErrorByName('mappings][new_mapping][browser_langcode', $this->t('Browser language codes must be unique.'));
            }
            elseif (preg_match('/[^a-z\\-]/', $data['browser_langcode'])) {
                $form_state->setErrorByName('mappings][new_mapping][browser_langcode', $this->t('Browser language codes can only contain lowercase letters and a hyphen(-).'));
            }
            $unique_values[$data['browser_langcode']] = $data['drupal_langcode'];
        }
    }
    // Check new mapping.
    $data = $form_state->getValue('new_mapping');
    if (!empty($data['browser_langcode'])) {
        // Make sure browser_langcode is unique.
        if (array_key_exists($data['browser_langcode'], $unique_values)) {
            $form_state->setErrorByName('mappings][' . $key . '][browser_langcode', $this->t('Browser language codes must be unique.'));
        }
        elseif (preg_match('/[^a-z\\-]/', $data['browser_langcode'])) {
            $form_state->setErrorByName('mappings][' . $key . '][browser_langcode', $this->t('Browser language codes can only contain lowercase letters and a hyphen(-).'));
        }
        $unique_values[$data['browser_langcode']] = $data['drupal_langcode'];
    }
    $form_state->set('mappings', $unique_values);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.