You are here

class LanguageNegotiationCookie in Language Cookie 8

Class for identifying language from a language cookie.

The recommended order is URL > Cookie > Language Selection Page, so weight is set to -5 by default so that it is lower than Language Selection Page (see https://www.drupal.org/project/language_selection_page), which has a weight of -4, and so that it higher than URL, which has a weight of -8.

Plugin annotation


@LanguageNegotiation(
  weight = -5,
  name = @Translation("Cookie"),
  description = @Translation("Determine the language from a cookie"),
  id = Drupal\language_cookie\Plugin\LanguageNegotiation\LanguageNegotiationCookie::METHOD_ID,
  config_route_name = "language_cookie.negotiation_cookie"
)

Hierarchy

Expanded class hierarchy of LanguageNegotiationCookie

6 files declare their use of LanguageNegotiationCookie
LanguageCookieConditionMethodIsValid.php in src/Plugin/LanguageCookieCondition/LanguageCookieConditionMethodIsValid.php
LanguageCookieLanguageSelectionPageTest.php in tests/src/Functional/LanguageCookieLanguageSelectionPageTest.php
LanguageCookieSubscriber.php in src/EventSubscriber/LanguageCookieSubscriber.php
LanguageCookieTestBase.php in tests/src/Functional/LanguageCookieTestBase.php
language_cookie.install in ./language_cookie.install
The install and update code for the language_cookie module.

... See full list

File

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

Namespace

Drupal\language_cookie\Plugin\LanguageNegotiation
View source
class LanguageNegotiationCookie extends LanguageNegotiationMethodBase implements ContainerFactoryPluginInterface {

  /**
   * The language negotiation method ID.
   */
  const METHOD_ID = 'language-cookie';

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The page cache kill switch.
   *
   * @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch
   */
  protected $pageCacheKillSwitch;

  /**
   * Constructs a new LanguageNegotiationCookie instance.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   * @param \Drupal\Core\PageCache\ResponsePolicy\KillSwitch $page_cache_kill_switch
   *   The page cache kill switch.
   */
  public function __construct(ConfigFactoryInterface $config_factory, KillSwitch $page_cache_kill_switch) {
    $this->configFactory = $config_factory;
    $this->pageCacheKillSwitch = $page_cache_kill_switch;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($container
      ->get('config.factory'), $container
      ->get('page_cache_kill_switch'));
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
LanguageNegotiationCookie::$configFactory protected property The configuration factory.
LanguageNegotiationCookie::$pageCacheKillSwitch protected property The page cache kill switch.
LanguageNegotiationCookie::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
LanguageNegotiationCookie::getLangcode public function Performs language negotiation. Overrides LanguageNegotiationMethodInterface::getLangcode
LanguageNegotiationCookie::METHOD_ID constant The language negotiation method ID.
LanguageNegotiationCookie::__construct public function Constructs a new LanguageNegotiationCookie instance.
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