You are here

public function EntityRepository::getActiveMultiple in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/EntityRepository.php \Drupal\Core\Entity\EntityRepository::getActiveMultiple()

Retrieves the active 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::getActiveMultiple

See also

getActive()

1 call to EntityRepository::getActiveMultiple()
EntityRepository::getActive in core/lib/Drupal/Core/Entity/EntityRepository.php
Retrieves the active entity variant matching the specified context.

File

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

Class

EntityRepository
Provides several mechanisms for retrieving entities.

Namespace

Drupal\Core\Entity

Code

public function getActiveMultiple($entity_type_id, array $entity_ids, array $contexts = NULL) {
  $active = [];
  if (!isset($contexts)) {
    $contexts = $this->contextRepository
      ->getAvailableContexts();
  }

  // @todo Consider implementing a more performant version of this logic fully
  //   supporting multiple entities in https://www.drupal.org/node/3031082.
  $langcode = $this->languageManager
    ->isMultilingual() ? $this
    ->getContentLanguageFromContexts($contexts) : $this->languageManager
    ->getDefaultLanguage()
    ->getId();
  $entities = $this->entityTypeManager
    ->getStorage($entity_type_id)
    ->loadMultiple($entity_ids);
  foreach ($entities as $id => $entity) {

    // Retrieve the fittest revision, if needed.
    if ($entity instanceof RevisionableInterface && $entity
      ->getEntityType()
      ->isRevisionable()) {
      $entity = $this
        ->getLatestTranslationAffectedRevision($entity, $langcode);
    }

    // Retrieve the fittest translation, if needed.
    if ($entity instanceof TranslatableInterface) {
      $entity = $this
        ->getTranslationFromContext($entity, $langcode);
    }
    $active[$id] = $entity;
  }
  return $active;
}