You are here

public function LanguageNegotiator::updateConfiguration in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/language/src/LanguageNegotiator.php \Drupal\language\LanguageNegotiator::updateConfiguration()
  2. 9 core/modules/language/src/LanguageNegotiator.php \Drupal\language\LanguageNegotiator::updateConfiguration()

File

core/modules/language/src/LanguageNegotiator.php, line 297

Class

LanguageNegotiator
Class responsible for performing language negotiation.

Namespace

Drupal\language

Code

public function updateConfiguration(array $types) {

  // Ensure that we are getting the defined language negotiation information.
  // An invocation of \Drupal\Core\Extension\ModuleInstaller::install() or
  // \Drupal\Core\Extension\ModuleInstaller::uninstall() could invalidate the
  // cached information.
  $this->negotiatorManager
    ->clearCachedDefinitions();
  $this->languageManager
    ->reset();
  $language_types = [];
  $language_types_info = $this->languageManager
    ->getDefinedLanguageTypesInfo();
  $method_definitions = $this
    ->getNegotiationMethods();
  foreach ($language_types_info as $type => $info) {
    $configurable = in_array($type, $types);

    // The default language negotiation settings, if available, are stored in
    // $info['fixed'].
    $has_default_settings = !empty($info['fixed']);

    // Check whether the language type is unlocked. Only the status of
    // unlocked language types can be toggled between configurable and
    // non-configurable.
    if (empty($info['locked'])) {
      if (!$configurable && !$has_default_settings) {

        // If we have an unlocked non-configurable language type without
        // default language negotiation settings, we use the values
        // negotiated for the interface language which, should always be
        // available.
        $method_weights = [
          LanguageNegotiationUI::METHOD_ID,
        ];
        $method_weights = array_flip($method_weights);
        $this
          ->saveConfiguration($type, $method_weights);
      }
    }
    else {

      // The language type is locked. Locked language types with default
      // settings are always considered non-configurable. In turn if default
      // settings are missing, the language type is always considered
      // configurable.
      // If the language type is locked we can just store its default language
      // negotiation settings if it has some, since it is not configurable.
      if ($has_default_settings) {
        $method_weights = [];

        // Default settings are in $info['fixed'].
        foreach ($info['fixed'] as $weight => $method_id) {
          if (isset($method_definitions[$method_id])) {
            $method_weights[$method_id] = $weight;
          }
        }
        $this
          ->saveConfiguration($type, $method_weights);
      }
      else {

        // It was missing default settings, so force it to be configurable.
        $configurable = TRUE;
      }
    }

    // Accumulate information for each language type so it can be saved later.
    $language_types[$type] = $configurable;
  }

  // Store the language type configuration.
  $config = [
    'configurable' => array_keys(array_filter($language_types)),
    'all' => array_keys($language_types),
  ];
  $this->languageManager
    ->saveLanguageTypesConfiguration($config);
}