You are here

class LanguageAccessNegotiationBrowser in Language access 8

Alternate language browser plugin that checks access to the languages.

Hierarchy

Expanded class hierarchy of LanguageAccessNegotiationBrowser

See also

\Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser

2 files declare their use of LanguageAccessNegotiationBrowser
LanguageAccessBrowserTest.php in tests/src/Kernel/LanguageAccessBrowserTest.php
language_access.module in ./language_access.module
Provide access permissions by language.

File

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

Namespace

Drupal\language_access\Plugin\LanguageNegotiation
View source
class LanguageAccessNegotiationBrowser extends LanguageNegotiationBrowser {

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LanguageAccessNegotiationBrowser::getLangcode public function Performs language negotiation. Overrides LanguageNegotiationBrowser::getLangcode
LanguageNegotiationBrowser::$pageCacheKillSwitch protected property The page cache disabling policy.
LanguageNegotiationBrowser::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
LanguageNegotiationBrowser::METHOD_ID constant The language negotiation method id.
LanguageNegotiationMethodBase::$config protected property The configuration factory.
LanguageNegotiationMethodBase::$currentUser protected property The current active user.
LanguageNegotiationMethodBase::$languageManager protected property The language manager.
LanguageNegotiationMethodBase::persist public function Notifies the plugin that the language code it returned has been accepted. Overrides LanguageNegotiationMethodInterface::persist 1
LanguageNegotiationMethodBase::setConfig public function Injects the configuration factory. Overrides LanguageNegotiationMethodInterface::setConfig
LanguageNegotiationMethodBase::setCurrentUser public function Injects the current user. Overrides LanguageNegotiationMethodInterface::setCurrentUser
LanguageNegotiationMethodBase::setLanguageManager public function Injects the language manager. Overrides LanguageNegotiationMethodInterface::setLanguageManager