Index: token_node.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/token/token_node.inc,v retrieving revision 1.5.4.2 diff -u -r1.5.4.2 token_node.inc --- token_node.inc 8 Nov 2007 14:48:00 -0000 1.5.4.2 +++ token_node.inc 19 Feb 2008 02:52:57 -0000 @@ -193,19 +193,59 @@ } } +/** + * Note: This is a replacement for the Drupal 5 book module function + * book_location(), which was dropped in Drupal 6. + * + * Given a node, this function returns an array of 'book node' objects + * representing the path in the book tree from the root to the + * parent of the given node. + * + * @param $node + * A book node object for which to compute the path. + * + * @return + * An array of book node objects representing the path nodes root to + * parent of the given node. Returns an empty array if the node does + * not exist or is not part of a book hierarchy. + */ +function _book_location($node) { + /* Get the menu_links row for a node - the p* columns have the hierarchy */ + $menuquery = 'SELECT m.p1,m.p2,m.p3,m.p4,m.p5,m.p6,m.p7,m.p8,m.p9 + FROM {book} b + INNER JOIN {menu_links} m ON b.mlid=m.mlid + WHERE b.nid = %d'; + /* For a book menu link, get the node id, title, and menu weight + * Provide a dummy parent value, to be filled in later */ + $nodequery = 'SELECT b.nid, n.title, 0 as parent, m.weight + FROM {book} b + INNER JOIN {node} n ON b.nid = n.nid + INNER JOIN {menu_links} m ON b.mlid = m.mlid + WHERE b.mlid = %d'; + /* By converting to array, we're able to iterate over the p* fields */ + $menu = (array)db_fetch_object(db_query(db_rewrite_sql($menuquery), $node->nid)); + for ($i = 1, $parentid = 0; $i <= 9 and $menu["p$i"] <> 0; $i++) { + $mlid = $menu["p$i"]; + $nodes[$i-1] = db_fetch_object(db_query(db_rewrite_sql($nodequery), $mlid)); + $nodes[$i-1]->parent = $parentid; + $parentid = $nodes[$i-1]->nid; + } + return $nodes; +} + // Handle book nodes. function book_token_values($type, $object = NULL, $options = array()) { if ($type == 'node') { $node = $object; $tokens = array(); - if ($node->parent) { - $path = book_location($node); + if ($node->book) { + $path = _book_location($node); $tokens['book'] = check_plain($path[0]->title); $tokens['book-raw'] = $path[0]->title; - $tokens['book_id'] = $node->parent; + $tokens['book_id'] = $path[0]->nid; - $bookhierarchy = book_location($node); + $bookhierarchy = $path; $bookpath = ''; $bookpath_raw = ''; foreach ($bookhierarchy as $bookelement) { @@ -225,6 +265,7 @@ $tokens['book'] = ''; $tokens['book_id'] = ''; $tokens['bookpath'] = ''; + $tokens['book-raw'] = ''; $tokens['bookpath-raw'] = ''; } return $tokens;