8.4.x Entity.php public static Entity::load($id)
8.0.x Entity.php public static Entity::load($id)
8.1.x Entity.php public static Entity::load($id)
8.2.x Entity.php public static Entity::load($id)
8.3.x Entity.php public static Entity::load($id)
8.5.x Entity.php Entity::load($id)

Loads an entity.

Parameters

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

Return value

static The entity object or NULL if there is no entity with the given ID.

Overrides EntityInterface::load

File

core/lib/Drupal/Core/Entity/Entity.php, line 497

Class

Entity
Defines a base entity class.

Namespace

Drupal\Core\Entity

Code

public static function load($id) {
  $entity_manager = \Drupal::entityManager();
  return $entity_manager->getStorage($entity_manager->getEntityTypeFromClass(get_called_class()))->load($id);
}

Comments

Insasse’s picture

entity_load is an awesome function it is a pitty that there is non equivalent function out there :(

ec0g’s picture

// $entity_type could be "node" "taxonomy_term" etc...

\Drupal::entityTypeManager()->getStorage($entity_type)->load($id);

a.dmitriiev’s picture

@Insasse, please read this: https://docs.acquia.com/article/lesson-62-loading-entities

You can do :

// Get a node storage object.
$node_storage = \Drupal::entityManager()->getStorage('node');

// Load a single node.
$node_storage->load($nid);

// Load multiple nodes.
$node_storage->loadMultiple($nids);

Andy_D’s picture

This method uses deprecated functions - I'm currently researching alternatives!

merauluka’s picture

The method still works. Just switch out entityManager() with entityTypeManager().

https://api.drupal.org/api/drupal/core!lib!Drupal.php/function/Drupal%3A...

Neograph734’s picture

Every entity in Drupal extends the Entity class. If you need to load a node, you can use Drupal\node\Entity\Node::load($nid); or Drupal\node\Entity\Node::loadMultiple($nids); This will call the above functions via class inheritance.

Or cleaner:

// Define usage at the top of your file.
use Drupal\node\Entity\Node;

// Later on there is no need to provide the entire namespace.
Node::load($nid);