You are here

public function EntityRepository::getCanonicalMultiple in Drupal 8

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

Retrieves the canonical entity variants matching the specified context.

Parameters

string $entity_type_id: The entity type identifier.

int[]|string[] $entity_ids: An array of entity identifiers.

\Drupal\Core\Plugin\Context\ContextInterface[] $contexts: (optional) An associative array of objects representing the contexts the entity will be edited in keyed by fully qualified context ID. Defaults to the currently available contexts.

Return value

\Drupal\Core\Entity\EntityInterface An array of entity object variants keyed by entity ID.

Overrides EntityRepositoryInterface::getCanonicalMultiple

See also

getCanonical()

1 call to EntityRepository::getCanonicalMultiple()
EntityRepository::getCanonical in core/lib/Drupal/Core/Entity/EntityRepository.php
Retrieves the canonical entity variant matching the specified context.

File

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

Class

EntityRepository
Provides several mechanisms for retrieving entities.

Namespace

Drupal\Core\Entity

Code

public function getCanonicalMultiple($entity_type_id, array $entity_ids, array $contexts = NULL) {
  $entities = $this->entityTypeManager
    ->getStorage($entity_type_id)
    ->loadMultiple($entity_ids);
  if (!$entities || !$this->languageManager
    ->isMultilingual()) {
    return $entities;
  }
  if (!isset($contexts)) {
    $contexts = $this->contextRepository
      ->getAvailableContexts();
  }

  // @todo Consider deprecating the legacy context operation altogether in
  //   https://www.drupal.org/node/3031124.
  $legacy_context = [];
  $key = static::CONTEXT_ID_LEGACY_CONTEXT_OPERATION;
  if (isset($contexts[$key])) {
    $legacy_context['operation'] = $contexts[$key]
      ->getContextValue();
  }
  $canonical = [];
  $langcode = $this
    ->getContentLanguageFromContexts($contexts);
  foreach ($entities as $id => $entity) {
    $canonical[$id] = $this
      ->getTranslationFromContext($entity, $langcode, $legacy_context);
  }
  return $canonical;
}