You are here

public function LanguageNegotiationCookie::getLangcode in Language Cookie 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 LanguageNegotiationMethodInterface::getLangcode

File

src/Plugin/LanguageNegotiation/LanguageNegotiationCookie.php, line 75

Class

LanguageNegotiationCookie
Class for identifying language from a language cookie.

Namespace

Drupal\language_cookie\Plugin\LanguageNegotiation

Code

public function getLangcode(Request $request = NULL) {
  $langcode = NULL;
  $config = $this->configFactory
    ->get('language_cookie.negotiation');
  $param = $config
    ->get('param');
  if ($request->cookies
    ->has($param) && in_array($request->cookies
    ->get($param), array_keys($this->languageManager
    ->getLanguages()))) {
    $langcode = $request->cookies
      ->get($param);

    // 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;
}