Loads a list of entities for the given entity type ID.
By providing the language code, the latest revisions affecting the specified translation (language code) will be returned.
string $entity_type_id: The entity type ID.
array $entity_ids: A list of entity IDs to load.
string|null $langcode: (optional) The language code. Defaults to source entity language.
\Drupal\Core\Entity\EntityInterface[] Returns a list of entities.
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
\Drupal\Component\Plugin\Exception\PluginNotFoundException
public static function loadMultiple($entity_type_id, array $entity_ids, $langcode = NULL) {
/** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
$storage = \Drupal::entityTypeManager()
->getStorage($entity_type_id);
$entities = $storage
->loadMultiple($entity_ids);
// Load the latest revision if the entity type is revisionable.
if ($storage
->getEntityType()
->isRevisionable() && $storage instanceof TranslatableRevisionableStorageInterface) {
foreach ($entities as $entity_id => $entity) {
// Use the specified langcode or fallback to the default language.
$translation_langcode = $langcode ?: $entity
->language()
->getId();
$revision_id = $storage
->getLatestTranslationAffectedRevisionId($entity
->id(), $translation_langcode);
// Get the pending revisions. If the returned revision ID is the same as
// the default one, there is no need for further checks.
if ($revision_id && $entity
->getRevisionId() != $revision_id) {
$revision = $storage
->loadRevision($revision_id);
// If the affected revision was the default one at some point, then it
// is an old revision that should be part of the already loaded
// default so we do not need to replace it here.
if (!$revision
->wasDefaultRevision()) {
$entities[$entity_id] = $revision;
}
}
}
}
return $entities;
}