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	24 Feb 2008 22:30:17 -0000
@@ -193,22 +193,62 @@
   }
 }
 
+/**
+ * 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));
+  /* Note we look at the next $menu item, so this node itself isn't included */
+  for ($i = 1, $parentid = 0; $i <= 9 and $menu['p'.($i+1)] <> 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 ($path = _book_location($node)) {
+      // Non-parent book nodes
       $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);
       $bookpath = '';
       $bookpath_raw = '';
-      foreach ($bookhierarchy as $bookelement) {
+      foreach ($path as $bookelement) {
         if ($bookpath == '') {
           $bookpath = check_plain($bookelement->title);
           $bookpath_raw = $bookelement->title;
@@ -220,11 +260,19 @@
       }
       $tokens['bookpath'] = $bookpath;
       $tokens['bookpath-raw'] = $bookpath_raw;
-    } 
-    else {
+    } elseif ($node->type == 'book') {
+      // Handle a book node type, which hasn't been placed in (or made
+      // the top of) a book
+      $tokens['book'] = check_plain($node->title);
+      $tokens['book-raw'] = $node->title;
+      $tokens['book_id'] = $node->nid;
+      $tokens['bookpath'] = $tokens['book'];
+      $tokens['bookpath-raw'] = $tokens['book-raw'];
+    } else {
       $tokens['book'] = '';
       $tokens['book_id'] = '';
       $tokens['bookpath'] = '';
+      $tokens['book-raw'] = '';
       $tokens['bookpath-raw'] = '';
     }
     return $tokens;
