function LanguagesCacheContext::getContext

Same name in other branches
  1. 9 core/lib/Drupal/Core/Cache/Context/LanguagesCacheContext.php \Drupal\Core\Cache\Context\LanguagesCacheContext::getContext()
  2. 10 core/lib/Drupal/Core/Cache/Context/LanguagesCacheContext.php \Drupal\Core\Cache\Context\LanguagesCacheContext::getContext()
  3. 11.x core/lib/Drupal/Core/Cache/Context/LanguagesCacheContext.php \Drupal\Core\Cache\Context\LanguagesCacheContext::getContext()

$type can be NULL, or one of the language types supported by the language manager, typically:

Throws

\RuntimeException In case an invalid language type is specified.

Overrides CalculatedCacheContextInterface::getContext

See also

\Drupal\Core\Language\LanguageManagerInterface::getLanguageTypes()

File

core/lib/Drupal/Core/Cache/Context/LanguagesCacheContext.php, line 51

Class

LanguagesCacheContext
Defines the LanguagesCacheContext service, for "per language" caching.

Namespace

Drupal\Core\Cache\Context

Code

public function getContext($type = NULL) {
    if ($type === NULL) {
        $context_parts = [];
        if ($this->languageManager
            ->isMultilingual()) {
            foreach ($this->languageManager
                ->getLanguageTypes() as $type) {
                $context_parts[] = $this->languageManager
                    ->getCurrentLanguage($type)
                    ->getId();
            }
        }
        else {
            $context_parts[] = $this->languageManager
                ->getCurrentLanguage()
                ->getId();
        }
        return implode(',', $context_parts);
    }
    else {
        $language_types = $this->languageManager
            ->getDefinedLanguageTypesInfo();
        if (!isset($language_types[$type])) {
            throw new \RuntimeException(sprintf('The language type "%s" is invalid.', $type));
        }
        return $this->languageManager
            ->getCurrentLanguage($type)
            ->getId();
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.