function nodeapi_example_node_load in Examples for Developers 7
Implements hook_node_load().
Loads the rating information if available for any of the nodes in the argument list.
Related topics
File
- nodeapi_example/
nodeapi_example.module, line 109 - Module implementation for nodeapi_example module.
Code
function nodeapi_example_node_load($nodes, $types) {
// We can use $types to figure out if we need to process any of these nodes.
$our_types = array();
foreach ($types as $type) {
if (variable_get('nodeapi_example_node_type_' . $type, FALSE)) {
$our_types[] = $type;
}
}
// Now $our_types contains all the types from $types that we want
// to deal with. If it's empty, we can safely return.
if (!count($our_types)) {
return;
}
// Now we need to make a list of revisions based on $our_types
foreach ($nodes as $node) {
// We are using the revision id instead of node id.
if (variable_get('nodeapi_example_node_type_' . $node->type, FALSE)) {
$vids[] = $node->vid;
}
}
// Check if we should load rating for any of the nodes.
if (!isset($vids) || !count($vids)) {
return;
}
// When we read, we don't care about the node->nid; we look for the right
// revision ID (node->vid).
$result = db_select('nodeapi_example', 'e')
->fields('e', array(
'nid',
'vid',
'rating',
))
->where('e.vid IN (:vids)', array(
':vids' => $vids,
))
->execute();
foreach ($result as $record) {
$nodes[$record->nid]->nodeapi_example_rating = $record->rating;
}
}