Load entities from the database.

Load entities from the database.

The entities are stored in a static memory cache, and will not require database access if loaded again during the same page request.

The actual loading is done through a class that has to implement the DrupalEntityControllerInterface interface. By default, DrupalDefaultEntityController is used. Entity types can specify that a different class should be used by setting the 'controller class' key in hook_entity_info(). These classes can either implement the DrupalEntityControllerInterface interface, or, most commonly, extend the DrupalDefaultEntityController class. See node_entity_info() and the NodeController in node.module as an example.

@todo Remove $conditions in Drupal 8.


bvanmeurs’s picture

"Instead, it is preferable to use EntityFieldQuery to retrieve a list of entity IDs loadable by this function."


  $query = new EntityFieldQuery();

    ->entityCondition('entity_type', 'artwork', '=')
    ->propertyCondition('type', 'painting', '=');

  $result = $query->execute();
  $aids = array();
  foreach($result['artwork'] as $record) {
    $aids[] = $record->aid;
  $artworks = entity_load('artwork', $aids);
mikeejt’s picture

You can easily get the ids from the query by getting the returned array's keys:

$ids = array_keys($result['artwork']);

Chris Gillis’s picture

->propertyCondition('relation_type', 'painting', '=');

kingandy’s picture

The propertyCondition() method adds a condition based on a column of the base table of the entity. The available columns will vary based on the entity, so you could both be right depending on the structure of the 'artwork' entity in your respective databases.

That said, it looks like we're talking about 'bundle type' fields, so in this case it would probably be more appropriate to use
->entityCondition('bundle', 'painting');

The entity bundle is almost always determined by a field on the base table, yes - 'type' or 'relation_type' - but using entityCondition() allows you to do so without actually looking at the database schema.

axel.rutz’s picture

rafinskipg’s picture

Is it possible to use entity load with fields?? I tried but i can't make it work

Simix’s picture

Yeah, put your fields in $conditions as array like this :
$items = entity_load('node', FALSE, array('type' => 'article', 'title' => 'mytitle'));

markie’s picture

You would want to use an EntityFieldQuery instead. See http://drupal.org/node/1343708

alanom’s picture

1) ...that calling entity_load() can cause that entity's cache to be updated (this happens in DrupalDefaultEntityController::load)

2) ...that if you want to reload of revert an entity to load it as it is on the database right now, not as it was when previously loaded or as it is in memory after being editted in memory, you don't need to use the final 'true' parameter which can have unintended consequences wiping caches of other entities of the same type, you can use entity_load_unchanged()

Spry_Julia’s picture

This says that the return value contains "An array of entity objects indexed by their ids. When no results are found, an empty array is returned."

Can anyone tell me where the properties of the entity object are documented? I've searched the API documentation, as well as Google, and been unable to find this so far. Thanks!

kerasai’s picture

There are no specific properties for entities, they will depend on what's defined in the hook_entity_info() of the module that implements the entity type. Other modules may add into the object through hooks, so what the entity object looks like will depend on what type of entity you're working with, fields defined for the entity, and the combination of modules that may hook in.

dmatamales’s picture

If you just want to look up a single entity ID, make sure you put it in as an array (at least in Drupal 7). I didn't read the parameters carefully enough, and that caused me a bit of grief. :P

      $entity = entity_load('field_collection_item', array('single_entity_id'));
amazingrando’s picture

Your advice that a single entity ID had to be an array just solved a headache for me. Thank you.

chrisroane’s picture

To add to this, if you are only getting one node, you can use this:
$test_node = reset(entity_load('node', array(8)));

Replace `8` with the node id. If you don't do this, entity_load() will return an array with the first row being 'node_id' => 'node_object'....makes it harder to work with when you know you are only dealing with one node.

VladSavitsky’s picture

Use entity_load_single() to load only one entity.

mikeytown2’s picture

DrupalDefaultEntityController::load as clicking on load currently does a search for all load functions.

rooby’s picture

If $conditions are deprecated, what would be the preferred way of loading a specific old revision of an entity.

For example, what is the non-conditions way of doing this:

$old_revision = entity_load('node', array(), array('vid' => 123));

EntityFieldQuery() can return revisions using age(), however it doesn't return full nodes so you then have to load the nodes, and then you need conditions, unless I'm missing something somewhere.

I will track down the issue where $conditions are removed and ask there too when I get some time.

rooby’s picture

To answer my own question: entity_revision_load().
In drupal 8 it is in core: entity_revision_load()
In drupal 7 it is in the Entity API module: entity_revision_load()

leewoodman’s picture

$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'my_bundle_name')
->fieldCondition('field_id', 'value', $id, '=')
->range(0, 1);

$result = $query->execute();
if (isset($result['node'])) {
$nid = array_keys($result['node']);
$node = entity_load_single('node', $nid);
drupal_set_message('The nid is ' . $node->nid);

rashidkhan’s picture

The functionality of this function has changed in Drupal 8. In Drupal 8 the entity_load will return a single entity so you don't need to use PHP reset function or other function go get a single Entity.
Instead there will be a new function in Drupal 8 called entity_load_multiple to load multiple entities if you need it just like user_load_multiple and node_load_multiple.
This can be seen here.

nicxvan’s picture

For Paragraphs

      $entity = entity_load('paragraphs_item', array('single_entity_id'));
Jlols’s picture

Does this return an array, or must I place it into an array?