Drupal - Which should I use and when: node_load() or entity_load()?
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, ...);
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.
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:
node_load()
doesn't use$vid
as second parameterentity_load()
andnode_load_multiple()
don't use$condition
as second parameter (and it was marked as deprecated already in Drupal 7)