| 7 node.api.php | hook_node_load($nodes, $types) |
| 8 node.api.php | hook_node_load($nodes, $types) |
Act on nodes being loaded from the database.
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_node_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.
This hook should only 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.
The $types parameter allows for your module to have an early return (for efficiency) if your module only supports 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.
Parameters
$nodes: An array of the nodes being loaded, keyed by nid.
$types: An array containing the types of the nodes.
For a detailed usage example, see nodeapi_example.module.
Related topics
7 functions implement hook_node_load()
File
- modules/
node/ node.api.php, line 550 - Hooks provided by the Node module.
Code
function hook_node_load($nodes, $types) {
$result = db_query('SELECT nid, foo FROM {mytable} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes)));
foreach ($result as $record) {
$nodes[$record->nid]->foo = $record->foo;
}
}
Login or register to post comments
Comments
For a detailed usage example,
http://api.drupal.org/api/examples/nodeapi_example--nodeapi_example.modu...
Infinite loop when call node_access() inside this hook
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().