You are here

public function LanguageDropdownForm::submitForm in Language Switcher Dropdown 8.2

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/LanguageDropdownForm.php, line 321

Class

LanguageDropdownForm
Language Switch Form.

Namespace

Drupal\lang_dropdown\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $language_code = $form_state
    ->getValue('lang_dropdown_select');
  $type = $form_state
    ->getValue('lang_dropdown_type');
  $tohome = $form_state
    ->getValue('lang_dropdown_tohome');
  $language_codes = $this->languageManager
    ->getLanguages();
  if (!array_key_exists($language_code, $language_codes)) {
    return;
  }
  $types = $this->languageManager
    ->getDefinedLanguageTypesInfo();
  if (!array_key_exists($type, $types)) {
    return;
  }
  $route = $this->pathMatcher
    ->isFrontPage() ? '<front>' : '<current>';
  $url = Url::fromRoute($route);
  $languages = $this->languageManager
    ->getLanguageSwitchLinks($type, $url);
  $language = $languages->links[$language_code];
  $newurl = isset($language['url']) && $tohome == 0 ? $language['url'] : Url::fromRoute('<front>');
  if (!isset($language['query'])) {
    $language['query'] = $this->requestStack->query
      ->all();
  }
  $url = new Url($newurl
    ->getRouteName(), $newurl
    ->getRouteParameters(), $language);
  $form_state
    ->setResponse(new TrustedRedirectResponse($url
    ->toString(), Response::HTTP_SEE_OTHER));
}