You are here

public function LanguageAccessNegotiationBrowser::getLangcode in Language access 8

Performs language negotiation.

Parameters

\Symfony\Component\HttpFoundation\Request $request: (optional) The current request. Defaults to NULL if it has not been initialized yet.

Return value

string A valid language code or FALSE if the negotiation was unsuccessful.

Overrides LanguageNegotiationBrowser::getLangcode

File

src/Plugin/LanguageNegotiation/LanguageAccessNegotiationBrowser.php, line 19

Class

LanguageAccessNegotiationBrowser
Alternate language browser plugin that checks access to the languages.

Namespace

Drupal\language_access\Plugin\LanguageNegotiation

Code

public function getLangcode(Request $request = NULL) {
  $langcode = NULL;
  if ($this->languageManager && $request && $request->server
    ->get('HTTP_ACCEPT_LANGUAGE')) {
    $http_accept_language = $request->server
      ->get('HTTP_ACCEPT_LANGUAGE');
    $langcodes = array_keys($this->languageManager
      ->getLanguages());
    foreach ($langcodes as $langcode) {
      if (!$this->currentUser
        ->hasPermission('access language ' . $langcode)) {
        $langcodes = array_diff($langcodes, [
          $langcode,
        ]);
      }
    }
    $mappings = $this->config
      ->get('language.mappings')
      ->get('map');
    $langcode = UserAgent::getBestMatchingLangcode($http_accept_language, $langcodes, $mappings);
  }

  // Internal page cache with multiple languages and browser negotiation
  // could lead to wrong cached sites. Therefore disabling the internal page
  // cache.
  // @todo Solve more elegantly in https://www.drupal.org/node/2430335.
  $this->pageCacheKillSwitch
    ->trigger();
  return $langcode;
}