LanguagesCacheContext.php in Drupal 10
File
core/lib/Drupal/Core/Cache/Context/LanguagesCacheContext.php
View source
<?php
namespace Drupal\Core\Cache\Context;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Language\LanguageManagerInterface;
class LanguagesCacheContext implements CalculatedCacheContextInterface {
protected $languageManager;
public function __construct(LanguageManagerInterface $language_manager) {
$this->languageManager = $language_manager;
}
public static function getLabel() {
return t('Language');
}
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();
}
}
public function getCacheableMetadata($type = NULL) {
return new CacheableMetadata();
}
}