function hook_node_load

7 node.api.php hook_node_load($nodes, $types)
8 node.api.php hook_node_load($nodes, $types)

Act on arbitrary nodes being loaded from the database.

This hook should be used to add information that is not in the node or node revisions table, not to replace information that is in these tables (which could interfere with the entity cache). For performance reasons, information for all available nodes should be loaded in a single query where possible.

This hook is invoked during node loading, which is handled by entity_load(), via classes NodeController and DrupalDefaultEntityController. After the node information is read from the database or the entity cache, hook_load() is invoked on the node's content type module, then field_attach_load_revision() or field_attach_load() is called, then hook_entity_load() is invoked on all implementing modules, and finally hook_node_load() is invoked on all implementing modules.

Parameters

$nodes: An array of the nodes being loaded, keyed by nid.

$types: An array containing the node types present in $nodes. Allows for an early return for modules that only support certain node types. However, if your module defines a content type, you can use hook_load() to respond to loading of just that content type.

For a detailed usage example, see nodeapi_example.module.

Related topics

7 functions implement hook_node_load()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

book_node_load in modules/book/book.module
Implements hook_node_load().
comment_node_load in modules/comment/comment.module
Implements hook_node_load().
entity_crud_hook_test_node_load in modules/simpletest/tests/entity_crud_hook_test.module
Implements hook_node_load().
forum_node_load in modules/forum/forum.module
Implements hook_node_load().
node_access_test_node_load in modules/node/tests/node_access_test.module
Implements hook_node_load().

... See full list

File

modules/node/node.api.php, line 554
Hooks provided by the Node module.

Code

function hook_node_load($nodes, $types) {
  // Decide whether any of $types are relevant to our purposes.
  if (count(array_intersect($types_we_want_to_process, $types))) {
    // Gather our extra data for each of these nodes.
    $result = db_query('SELECT nid, foo FROM {mytable} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes)));
    // Add our extra data to the node objects.
    foreach ($result as $record) {
      $nodes[$record->nid]->foo = $record->foo;
    }
  }
}

Comments

Calling node_access() inside hook_node_load() may cause infinite loop, when some implementation of hook_node_access() calling node_load() or entity_load().

For example, in Orgainc Groups module, og_node_access() call og_get_group() function, that may call og_load(), then og_load_mutiple() and finally entity_load().

It's okay to call node_load() and entity_load() inside hook_node_access().

Hi every body.
can I alter this hook in my module?
How?
thanks.

pooya13662 - you don't need to alter it. Hooks are functions that you create in your module to hook into this function. So, if you create a module named mymodule and in the module create a function name function mymodule_node_load() the function would be called any time that a node is being loaded. You can then do whatever you like to the data.

It operates on plurality of nodes... should the name reflect it?

The example code will have no effect as the $nodes arg is not declared as a reference.

reply links are shown only for comments.

(basically.)

The individual objects within the $nodes array will retain whatever modifications you make to them.

Using &$nodes would be useful if your function were adding or removing objects from the array, and those additions/removals needed to be persistent outside your function. But that's not what this hook is for.

I have the need to run node_load inside of hook_noad_load, which runs out of memory as one might expect. Anyone know a way to get around this recursive behavior?