function NegotiationConfigureForm::submitForm

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

Overrides ConfigFormBase::submitForm

File

core/modules/language/src/Form/NegotiationConfigureForm.php, line 159

Class

NegotiationConfigureForm
Configure the selected language negotiation method for this site.

Namespace

Drupal\language\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
    $configurable_types = $form['#language_types'];
    $stored_values = $this->languageTypes
        ->get('configurable');
    $customized = [];
    $method_weights_type = [];
    foreach ($configurable_types as $type) {
        $customized[$type] = in_array($type, $stored_values);
        $method_weights = [];
        $enabled_methods = $form_state->getValue([
            $type,
            'enabled',
        ]);
        $enabled_methods[LanguageNegotiationSelected::METHOD_ID] = TRUE;
        $method_weights_input = $form_state->getValue([
            $type,
            'weight',
        ]);
        if ($form_state->hasValue([
            $type,
            'configurable',
        ])) {
            $customized[$type] = !$form_state->isValueEmpty([
                $type,
                'configurable',
            ]);
        }
        foreach ($method_weights_input as $method_id => $weight) {
            if ($enabled_methods[$method_id]) {
                $method_weights[$method_id] = $weight;
            }
        }
        $method_weights_type[$type] = $method_weights;
        $this->languageTypes
            ->set('negotiation.' . $type . '.method_weights', $method_weights_input)
            ->save();
    }
    // Update non-configurable language types and the related language
    // negotiation configuration.
    $this->negotiator
        ->updateConfiguration(array_keys(array_filter($customized)));
    // Update the language negotiations after setting the configurability.
    foreach ($method_weights_type as $type => $method_weights) {
        $this->negotiator
            ->saveConfiguration($type, $method_weights);
    }
    // Clear block definitions cache since the available blocks and their names
    // may have been changed based on the configurable types.
    if ($this->blockStorage) {
        // If there is an active language switcher for a language type that has
        // been made not configurable, deactivate it first.
        $non_configurable = array_keys(array_diff($customized, array_filter($customized)));
        $this->disableLanguageSwitcher($non_configurable);
    }
    $this->blockManager
        ->clearCachedDefinitions();
    $form_state->setRedirect('language.negotiation');
    $this->messenger()
        ->addStatus($this->t('Language detection configuration saved.'));
}

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