You are here

public function ContentEntityStorageBase::loadUnchanged in Drupal 8

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

Loads an unchanged entity from the database.

@todo Remove this method once we have a reliable way to retrieve the unchanged entity from the entity object.

Parameters

mixed $id: The ID of the entity to load.

Return value

\Drupal\Core\Entity\EntityInterface|null The unchanged entity, or NULL if the entity cannot be loaded.

Overrides EntityStorageBase::loadUnchanged

File

core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php, line 1064

Class

ContentEntityStorageBase
Base class for content entity storage handlers.

Namespace

Drupal\Core\Entity

Code

public function loadUnchanged($id) {
  $entities = [];
  $ids = [
    $id,
  ];

  // The cache invalidation in the parent has the side effect that loading the
  // same entity again during the save process (for example in
  // hook_entity_presave()) will load the unchanged entity. Simulate this
  // by explicitly removing the entity from the static cache.
  parent::resetCache($ids);

  // Gather entities from a 'preload' hook. This hook can be used by modules
  // that need, for example, to return a different revision than the default
  // one for revisionable entity types.
  $preloaded_entities = $this
    ->preLoad($ids);
  if (!empty($preloaded_entities)) {
    $entities += $preloaded_entities;
  }

  // The default implementation in the parent class unsets the current cache
  // and then reloads the entity. That is slow, especially if this is done
  // repeatedly in the same request, e.g. when validating and then saving
  // an entity. Optimize this for content entities by trying to load them
  // directly from the persistent cache again, as in contrast to the static
  // cache the persistent one will never be changed until the entity is saved.
  $entities += $this
    ->getFromPersistentCache($ids);
  if (!$entities) {
    $entities[$id] = $this
      ->load($id);
  }
  else {

    // As the entities are put into the persistent cache before the post load
    // has been executed we have to execute it if we have retrieved the
    // entity directly from the persistent cache.
    $this
      ->postLoad($entities);

    // As we've removed the entity from the static cache already we have to
    // put the loaded unchanged entity there to simulate the behavior of the
    // parent.
    $this
      ->setStaticCache($entities);
  }
  return $entities[$id];
}