function EntityRepository::getContentLanguageFromContexts

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/EntityRepository.php \Drupal\Core\Entity\EntityRepository::getContentLanguageFromContexts()
  2. 8.9.x core/lib/Drupal/Core/Entity/EntityRepository.php \Drupal\Core\Entity\EntityRepository::getContentLanguageFromContexts()
  3. 10 core/lib/Drupal/Core/Entity/EntityRepository.php \Drupal\Core\Entity\EntityRepository::getContentLanguageFromContexts()

Retrieves the current content language from the specified contexts.

This is a BC layer to support plugin context system identifiers, the langcode key should be used instead and is preferred when given.

@internal

Parameters

string[] $contexts: An array of context items.

Return value

string|null A language code or NULL if no language context was provided.

2 calls to EntityRepository::getContentLanguageFromContexts()
EntityRepository::getActiveMultiple in core/lib/Drupal/Core/Entity/EntityRepository.php
Retrieves the active entity variants matching the specified context.
EntityRepository::getCanonicalMultiple in core/lib/Drupal/Core/Entity/EntityRepository.php
Retrieves the canonical entity variants matching the specified context.

File

core/lib/Drupal/Core/Entity/EntityRepository.php, line 222

Class

EntityRepository
Provides several mechanisms for retrieving entities.

Namespace

Drupal\Core\Entity

Code

protected function getContentLanguageFromContexts(array $contexts) {
    if (isset($contexts['langcode'])) {
        return $contexts['langcode'];
    }
    // Content language might not be configurable, in which case we need to fall
    // back to a configurable language type.
    foreach ([
        LanguageInterface::TYPE_CONTENT,
        LanguageInterface::TYPE_INTERFACE,
    ] as $language_type) {
        $context_id = '@language.current_language_context:' . $language_type;
        if (isset($contexts[$context_id]) && $contexts[$context_id] instanceof ContextInterface) {
            @trigger_error('Providing the language as ' . $context_id . ' context to EntityRepository is deprecated in drupal:10.3.0 and is removed from drupal:12.0.0. Use the langcode key instead. See https://www.drupal.org/node/3437685', E_USER_DEPRECATED);
            return $contexts[$context_id]->getContextValue()
                ->getId();
        }
    }
    return $this->languageManager
        ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
        ->getId();
}

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