You are here

class CurrentLanguageContext in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Language/ContextProvider/CurrentLanguageContext.php \Drupal\Core\Language\ContextProvider\CurrentLanguageContext

Sets the current language as a context.

Hierarchy

Expanded class hierarchy of CurrentLanguageContext

1 string reference to 'CurrentLanguageContext'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses CurrentLanguageContext
language.current_language_context in core/core.services.yml
Drupal\Core\Language\ContextProvider\CurrentLanguageContext

File

core/lib/Drupal/Core/Language/ContextProvider/CurrentLanguageContext.php, line 15

Namespace

Drupal\Core\Language\ContextProvider
View source
class CurrentLanguageContext implements ContextProviderInterface {
  use StringTranslationTrait;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * Constructs a new CurrentLanguageContext.
   *
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   */
  public function __construct(LanguageManagerInterface $language_manager) {
    $this->languageManager = $language_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function getRuntimeContexts(array $unqualified_context_ids) {

    // Add a context for each language type.
    $language_types = $this->languageManager
      ->getLanguageTypes();
    $info = $this->languageManager
      ->getDefinedLanguageTypesInfo();
    if ($unqualified_context_ids) {
      foreach ($unqualified_context_ids as $unqualified_context_id) {
        if (array_search($unqualified_context_id, $language_types) === FALSE) {
          unset($language_types[$unqualified_context_id]);
        }
      }
    }
    $result = [];
    foreach ($language_types as $type_key) {
      if (isset($info[$type_key]['name'])) {
        $context = new Context(new ContextDefinition('language', $info[$type_key]['name']), $this->languageManager
          ->getCurrentLanguage($type_key));
        $cacheability = new CacheableMetadata();
        $cacheability
          ->setCacheContexts([
          'languages:' . $type_key,
        ]);
        $context
          ->addCacheableDependency($cacheability);
        $result[$type_key] = $context;
      }
    }
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableContexts() {
    return $this
      ->getRuntimeContexts([]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CurrentLanguageContext::$languageManager protected property The language manager.
CurrentLanguageContext::getAvailableContexts public function Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface::getAvailableContexts
CurrentLanguageContext::getRuntimeContexts public function Gets runtime context values for the given context IDs. Overrides ContextProviderInterface::getRuntimeContexts
CurrentLanguageContext::__construct public function Constructs a new CurrentLanguageContext.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.