1 node_hooks_example.module node_hooks_example_node_load($nodes, $types)

Implements hook_node_load().

Loads the rating information if available for any of the nodes in the argument list.

Related topics

File

modules/examples/node_hooks_example/node_hooks_example.module, line 108
Hook implementations for the Node Hooks Example module.

Code

function node_hooks_example_node_load($nodes, $types) {
  $config = config('node_hooks_example.settings');

  // We can use $types to figure out if we need to process any of these nodes.
  $our_types = array();
  foreach ($types as $type) {
    if ($config->get('node_hooks_example_node_type_' . $type)) {
      $our_types[] = $type;
    }
  }

  // $our_types contains all the types from $types that we want to deal with. If
  // it's empty, we can return.
  if (!count($our_types)) {
    return;
  }

  // Make a list of revisions based on $our_types.
  foreach ($nodes as $node) {
    // We use the revision ID instead of the node ID.
    if ($config->get('node_hooks_example_node_type_' . $node->type)) {
      $vids[] = $node->vid;
    }
  }
  // Verify we should load the rating for any node.
  if (!isset($vids) || !count($vids)) {
    return;
  }

  // When we read, we don't care about the node ID (the nid column); we look for
  // the right revision ID (the vid column).
  $result = db_select('node_hooks_example', 'e')
    ->fields('e', array('nid', 'vid', 'rating'))
    ->where('e.vid IN (:vids)', array(':vids' => $vids))
    ->execute();

  foreach ($result as $record) {
    $nodes[$record->nid]->node_hooks_example_rating = $record->rating;
  }
}