You are here

public function DomainLangNegotiationConfigureForm::submitForm in Domain Lang 8

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 NegotiationConfigureForm::submitForm

File

src/Form/DomainLangNegotiationConfigureForm.php, line 142

Class

DomainLangNegotiationConfigureForm
Configure the selected language negotiation method for this site.

Namespace

Drupal\domain_lang\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->domainLangHandler
    ->updateConfiguration(array_keys(array_filter($customized)));

  // Update the language negotiations after setting the configurability.
  foreach ($method_weights_type as $type => $method_weights) {
    $this->domainLangHandler
      ->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();
  $this
    ->messenger()
    ->addStatus($this
    ->t('Language detection configuration saved.'));
}