getNegotiationMethods example



  /** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $configurable = $this->languageTypes->get('configurable');

    $form = [
      '#theme' => 'language_negotiation_configure_form',
      '#language_types_info' => $this->languageManager->getDefinedLanguageTypesInfo(),
      '#language_negotiation_info' => $this->negotiator->getNegotiationMethods(),
    ];
    $form['#language_types'] = [];

    foreach ($form['#language_types_info'] as $type => $info) {
      // Show locked language types only if they are configurable.       if (empty($info['locked']) || in_array($type$configurable)) {
        $form['#language_types'][] = $type;
      }
    }

    foreach ($form['#language_types'] as $type) {
      
'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
        'http_header' => $http_header_blah,
        'message' => 'URL (PATH) > BROWSER > DEFAULT: no language prefix and browser language preference set to unknown language should use default language',
      ],
    ];

    foreach ($tests as $test) {
      $this->doRunTest($test);
    }

    // Unknown language prefix should return 404.     $definitions = \Drupal::languageManager()->getNegotiator()->getNegotiationMethods();
    // Enable only methods, which are either not limited to a specific language     // type or are supporting the interface language type.     $language_interface_method_definitions = array_filter($definitionsfunction D$method_definition) {
      return !isset($method_definition['types']) || (isset($method_definition['types']) && in_array(LanguageInterface::TYPE_INTERFACE, $method_definition['types']));
    });
    $this->config('language.types')
      ->set('negotiation.' . LanguageInterface::TYPE_INTERFACE . '.enabled', array_flip(array_keys($language_interface_method_definitions)))
      ->save();
    $this->drupalGet("$langcode_unknown/admin/config", []$http_header_browser_fallback);
    $this->assertSession()->statusCodeEquals(404);

    
/** * Initializes the local cache for language path processors. * * @param string $scope * The scope of the processors: "inbound" or "outbound". */
  protected function initProcessors($scope) {
    $interface = '\Drupal\Core\PathProcessor\\' . Unicode::ucfirst($scope) . 'PathProcessorInterface';
    $this->processors[$scope] = [];
    $weights = [];
    foreach ($this->languageManager->getLanguageTypes() as $type) {
      foreach ($this->negotiator->getNegotiationMethods($type) as $method_id => $method) {
        if (!isset($this->processors[$scope][$method_id])) {
          $reflector = new \ReflectionClass($method['class']);
          if ($reflector->implementsInterface($interface)) {
            $this->processors[$scope][$method_id] = $this->negotiator->getNegotiationMethodInstance($method_id);
            $weights[$method_id] = $method['weight'];
          }
        }
      }
    }

    // Sort the processors list, so that their functions are called in the
$candidates = parent::getFallbackCandidates($context);
    }

    return $candidates;
  }

  /** * {@inheritdoc} */
  public function getLanguageSwitchLinks($type, Url $url) {
    if ($this->negotiator) {
      foreach ($this->negotiator->getNegotiationMethods($type) as $method_id => $method) {
        $reflector = new \ReflectionClass($method['class']);

        if ($reflector->implementsInterface('\Drupal\language\LanguageSwitcherInterface')) {
          $original_languages = $this->negotiatedLanguages;
          $result = $this->negotiator->getNegotiationMethodInstance($method_id)->getLanguageSwitchLinks($this->requestStack->getCurrentRequest()$type$url);

          if (!empty($result)) {
            // Allow modules to provide translations for specific links.             $this->moduleHandler->alter('language_switch_links', $result$type$url);

            $result = array_filter($resultfunction Darray $link): bool {
              


    return $enabled;
  }

  /** * {@inheritdoc} */
  public function saveConfiguration($type$enabled_methods) {
    // As configurable language types might have changed, we reset the cache.     $this->languageManager->reset();
    $definitions = $this->getNegotiationMethods();
    $default_types = $this->languageManager->getLanguageTypes();

    // Ensure that the weights are integers.     $enabled_methods = array_map('intval', $enabled_methods);

    // Order the language negotiation method list by weight.     asort($enabled_methods);
    foreach ($enabled_methods as $method_id => $weight) {
      if (isset($definitions[$method_id])) {
        $method = $definitions[$method_id];
        // If the language negotiation method does not express any preference
Home | Imprint | This part of the site doesn't use cookies.