You are here

public function NegotiationConfigureForm::submitForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/language/src/Form/NegotiationConfigureForm.php \Drupal\language\Form\NegotiationConfigureForm::submitForm()

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 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.'));
}