class LanguagesCacheContext

Same name in other branches
  1. 9 core/lib/Drupal/Core/Cache/Context/LanguagesCacheContext.php \Drupal\Core\Cache\Context\LanguagesCacheContext
  2. 8.9.x core/lib/Drupal/Core/Cache/Context/LanguagesCacheContext.php \Drupal\Core\Cache\Context\LanguagesCacheContext
  3. 10 core/lib/Drupal/Core/Cache/Context/LanguagesCacheContext.php \Drupal\Core\Cache\Context\LanguagesCacheContext

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

Hierarchy

Expanded class hierarchy of LanguagesCacheContext

File

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

Namespace

Drupal\Core\Cache\Context
View source
class LanguagesCacheContext implements CalculatedCacheContextInterface {
    
    /**
     * The language manager.
     *
     * @var \Drupal\Core\Language\LanguageManagerInterface
     */
    protected $languageManager;
    
    /**
     * Constructs a new LanguagesCacheContext service.
     *
     * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
     *   The language manager.
     */
    public function __construct(LanguageManagerInterface $language_manager) {
        $this->languageManager = $language_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getLabel() {
        return t('Language');
    }
    
    /**
     * {@inheritdoc}
     *
     * $type can be NULL, or one of the language types supported by the language
     * manager, typically:
     * - LanguageInterface::TYPE_INTERFACE
     * - LanguageInterface::TYPE_CONTENT
     * - LanguageInterface::TYPE_URL
     *
     * @see \Drupal\Core\Language\LanguageManagerInterface::getLanguageTypes()
     *
     * @throws \RuntimeException
     *   In case an invalid language type is specified.
     */
    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();
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheableMetadata($type = NULL) {
        return new CacheableMetadata();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
LanguagesCacheContext::$languageManager protected property The language manager.
LanguagesCacheContext::getCacheableMetadata public function Gets the cacheability metadata for the context based on the parameter value. Overrides CalculatedCacheContextInterface::getCacheableMetadata
LanguagesCacheContext::getContext public function $type can be NULL, or one of the language types supported by the language
manager, typically:
Overrides CalculatedCacheContextInterface::getContext
LanguagesCacheContext::getLabel public static function Returns the label of the cache context. Overrides CalculatedCacheContextInterface::getLabel
LanguagesCacheContext::__construct public function Constructs a new LanguagesCacheContext service.

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