24

Title says it all.

I understand that entity_load provides a performant and efficient means to load multiple nodes whilst reducing database queries, so I presume in that instance I should be using entity_load(). But when I just want one node, which should I use? And will node_load() be deprecated in d8 in favour of the more generic entity_load() api?

I guess the same question goes for the familiar user_load() too.

32

node_load is just a fancy wrapper for entity_load.

That is node_load calls node_load_multiple which is a wrapper for entity_load. This means that performance wise there isn't really much of a difference, which function you use, unless you have to load a lot of nodes.

node_load has many advantages over entity_load

  • It's simpler
  • It increases code readability
  • it returns the node object instead of an array with the node object(s)

So in all, node_load is a convenience that you always should use whenever possible, then resort to node_load_multiple for cases where you need to load many more nodes.

entity_load should be used for loading entities when you don't know the type, or have it stored in a variable, in which case you can do

entity_load($type, ...);
  • 1
    after all node_load() is what we are used to. – Sivaji Jun 24 '11 at 16:44
9

Use node_load() when you have a single node to load and node_load_multiple() when you have more of them. Use entity_load() directly when you don't know the entity type in advance, for example entity_load($type, array($id)).

Note that entity_load() is inconsistently named, it should actually be entity_load_multiple(), as it allows to load multiple results and returns an array of results.

4

node_load() is the function that existed before the entity API was introduced in Drupal 7. The difference with entity_load() is that it accepts parameters that are specific for the loading a node object, such as $vid. The function calls node_load_multiple(); it is helpful for compatibility reason, and because it requires less parameters.

$node = node_load($nid);
$nodes = entity_load('node', array($nid));

In Drupal 8, node_load(), node_load_multiple() and entity_load() are deprecated. This they are removed when Drupal 9 will have its stable release).
Furthermore, in Drupal 8 calls to those functions need to be changed, since in Drupal 8:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.