function EntityStorageBase::loadMultiple
Loads one or more entities.
Parameters
array<string|int>|null $ids: An array of entity IDs, or NULL to load all entities.
Return value
\Drupal\Core\Entity\EntityInterface[] An array of successfully loaded objects indexed by their IDs. Returns an empty array if no matching entities are found.
Overrides EntityStorageInterface::loadMultiple
11 calls to EntityStorageBase::loadMultiple()
- CommentStorage::loadThread in core/
modules/ comment/ src/ CommentStorage.php - To display threaded comments in the correct order we keep a 'thread' field and order by that value. This field keeps this data in a way which is easy to update and convenient to use.
- ConfigEntityStorage::loadMultipleOverrideFree in core/
lib/ Drupal/ Core/ Config/ Entity/ ConfigEntityStorage.php - Loads one or more entities in their original form without overrides.
- EntityStorageBase::load in core/
lib/ Drupal/ Core/ Entity/ EntityStorageBase.php - Loads one entity.
- EntityStorageBase::loadByProperties in core/
lib/ Drupal/ Core/ Entity/ EntityStorageBase.php - Load entities by their property values without any access checks.
- FieldConfigStorage::loadByProperties in core/
modules/ field/ src/ FieldConfigStorage.php - Load entities by their property values without any access checks.
1 method overrides EntityStorageBase::loadMultiple()
- ContentEntityNullStorage::loadMultiple in core/
lib/ Drupal/ Core/ Entity/ ContentEntityNullStorage.php - Loads one or more entities.
File
-
core/
lib/ Drupal/ Core/ Entity/ EntityStorageBase.php, line 274
Class
- EntityStorageBase
- A base entity storage class.
Namespace
Drupal\Core\EntityCode
public function loadMultiple(?array $ids = NULL) {
$entities = [];
$preloaded_entities = [];
// Create a new variable which is either a prepared version of the $ids
// array for later comparison with the entity cache, or FALSE if no $ids
// were passed. The $ids array is reduced as items are loaded from cache,
// and we need to know if it is empty for this reason to avoid querying the
// database when all requested entities are loaded from cache.
$flipped_ids = $ids ? array_flip($ids) : FALSE;
// Try to load entities from the static cache, if the entity type supports
// static caching.
if ($ids && $this->entityType
->isStaticallyCacheable()) {
$entities += $this->getFromStaticCache($ids);
// If any entities were in the static cache remove them from the
// remaining IDs.
$ids = array_diff($ids, array_keys($entities));
$fiber = \Fiber::getCurrent();
if ($ids && $fiber !== NULL) {
// Before suspending the fiber, add the IDs passed in to the full list
// of entities to load, so that another call can load everything at
// once.
$this->entityIdsToLoad = array_unique(array_merge($this->entityIdsToLoad, $ids));
$fiber->suspend(FiberResumeType::Immediate);
// If all the IDs we need to return have already been loaded into the
// static cache, ignore any additionally requested entities here since
// deferring these may allow them to be loaded with more other entities
// later.
$entities += $this->getFromStaticCache($ids);
$ids = array_diff($ids, array_keys($entities));
// Otherwise load additional entities now.
if ($ids && $this->entityIdsToLoad) {
$ids = array_unique(array_merge($ids, $this->entityIdsToLoad));
$this->entityIdsToLoad = [];
}
}
}
// Try to gather any remaining entities from a 'preload' method. This method
// can invoke a hook to be used by modules that need, for example, to swap
// the default revision of an entity with a different one. Even though the
// base entity storage class does not actually invoke any preload hooks, we
// need to call the method here so we can add the pre-loaded entity objects
// to the static cache below. If all the entities were fetched from the
// static cache, skip this step.
if ($ids === NULL || $ids) {
$preloaded_entities = $this->preLoad($ids);
}
if (!empty($preloaded_entities)) {
$entities += $preloaded_entities;
// If any entities were pre-loaded, remove them from the IDs still to
// load.
$ids = array_diff($ids, array_keys($entities));
// Add pre-loaded entities to the cache.
$this->setStaticCache($preloaded_entities);
}
// Load any remaining entities from the database. This is the case if $ids
// is set to NULL (so we load all entities) or if there are any IDs left to
// load.
if ($ids === NULL || $ids) {
$queried_entities = $this->doLoadMultiple($ids);
}
// Pass all entities loaded from the database through $this->postLoad(),
// which attaches fields (if supported by the entity type) and calls the
// entity type specific load callback, for example hook_node_load().
if (!empty($queried_entities)) {
$this->postLoad($queried_entities);
$entities += $queried_entities;
// Add queried entities to the cache.
$this->setStaticCache($queried_entities);
}
if ($flipped_ids) {
// When IDs were passed in, ensure only entities that were loaded by this
// specific method call (e.g. not for other Fibers) are returned, and that
// any entities that could not be loaded are removed.
foreach ($flipped_ids as $entity_id => $value) {
if (isset($entities[$entity_id])) {
$flipped_ids[$entity_id] = $entities[$entity_id];
}
else {
unset($flipped_ids[$entity_id]);
}
}
$entities = $flipped_ids;
}
return $entities;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.