--- node.api.php 2008-11-24 19:48:54.000000000 +0000 +++ nodes.api.php 2008-11-24 19:47:07.000000000 +0000 @@ -252,6 +252,51 @@ function hook_nodeapi(&$node, $op, $a3 = } /** + * Act on node objects when loaded. + * + * This hook allows you to add information to node objects when loaded from the + * database. It takes an array of nodes as its first parameter, so that if + * possible, information for all available nodes can beloaded in a single query. + * You can also make use of the $types array to return early if none of the node + * types passed in will be affected by your module. + * Due to the internal cache in node_multiple_load(), you should not use this + * hook to modify any information returned from the {node} table itself since + * this will affect the way nodes are returned from the cache. The hook should + * operate directly on the node objects, so there is no return value. + * + * @see comment_nodeapi_load() + * @see taxonomy_nodeapi_load() + * + * @param $nodes + * An array of node objects indexed by nid. + * @param $types + * An array containing the types of the nodes. + */ +function hook_nodeapi_load($nodes, $types) { + // Get an array of tid, vid associations ordered by vocabulary and term + // weight. + $tids = taxonomy_get_tids_from_nodes($nodes); + + // Build an array consisting of only the unique tids. + $term_ids = array(); + foreach ($tids as $term) { + $term_ids[$term->tid] = $term->tid; + } + // Load the full term objects for these tids. + $terms = taxonomy_term_load_multiple($term_ids); + foreach ($tids as $term) { + $nodes[$term->nid]->taxonomy[$term->tid] = $terms[$term->tid]; + } + foreach ($nodes as $node) { + if (!isset($nodes[$node->nid]->taxonomy)) { + $node->taxonomy = array(); + } + } +} + + + +/** * @file * These hooks are defined by node modules, modules that define a new kind * of node. @@ -537,25 +582,22 @@ function hook_insert($node) { * Load node-type-specific information. * * This is a hook used by node modules. It is called to allow the module - * a chance to load extra information that it stores about a node, or - * possibly replace already loaded information - which can be dangerous. - * - * @param $node - * The node being loaded. At call time, node.module has already loaded - * the basic information about the node, such as its node ID (nid), - * title, and body. - * @return - * An object containing properties of the node being loaded. This will - * be merged with the passed-in $node to result in an object containing - * a set of properties resulting from adding the extra properties to - * the passed-in ones, and overwriting the passed-in ones with the - * extra properties if they have the same name as passed-in properties. + * a chance to load extra information that it stores about a node. The hook + * should not be used to replace information from the core {node} table since + * this may interfere with the way nodes are fetched from cache. + * + * @param $nodes + * An array of the nodes being loaded, keyed by nid. At call time, + * node.module has already loaded the basic information about the nodes, such + * as node ID (nid), title, and body. * * For a detailed usage example, see node_example.module. */ -function hook_load($node) { - $additions = db_fetch_object(db_query('SELECT * FROM {mytable} WHERE vid = %d', $node->vid)); - return $additions; +function hook_load($nodes) { + $result = db_fetch_object(db_query('SELECT nid, foo FROM {mytable} WHERE nid IN (' . db_placeholders(array_keys($nodes)) . ')', array_keys($nodes))); + foreach ($result as $record) { + $nodes[$record->nid]->foo = $record->foo; + } } /**