1 nodeapi_example.module nodeapi_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/nodeapi_example/nodeapi_example.module, line 119
Module implementation for nodeapi_example module.

Code

function nodeapi_example_node_load($nodes, $types) {
  // We load the configuration file.
  $config = config('nodeapi_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('nodeapi_example_node_type_' . $type)) {
      $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 ($config->get('nodeapi_example_node_type_' . $node->type)) {
      $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;
  }
}