--- token/token_node.inc	Thu Nov  8 07:48:00 2007
+++ token_rs/token_node.inc	Wed Apr  9 15:06:40 2008
@@ -51,35 +51,34 @@ function node_token_values($type, $objec
       }
       
       // Now get the menu related information.
-      global $_menu;
-      $trail = array();
-      $trail_raw = array();
-      $original_mid = token_menu_get_mid('node/'.$node->nid);
-      
-      $mid = $original_mid;
-      while ($mid && $_menu['visible'][$mid] && $_menu['visible'][$mid]['pid'] != 0) {
-        array_unshift($trail, check_plain($_menu['visible'][$mid]['title']));
-        $mid = $_menu['visible'][$mid]['pid'];
-      }
-
-      // One more time, unfiltered
-      $mid = $original_mid;
-      while ($mid && $_menu['visible'][$mid] && $_menu['visible'][$mid]['pid'] != 0) {
-        array_unshift($trail_raw, $_menu['visible'][$mid]['title']);
-        $mid = $_menu['visible'][$mid]['pid'];
-      }
-
-      if (isset($trail)) {
-        $values['menupath']     = implode('/', $trail);
-        $values['menupath-raw'] = implode('/', $trail_raw);
-        $values['menu']         = check_plain($_menu['visible'][$mid]['title']);
-        $values['menu-raw']     = $_menu['visible'][$mid]['title'];
+      if ($node->menu['mlid'] or $node->menu['plid']) {
+        $menus = menu_get_menus();
+        $menu_name = $node->menu['menu_name'];
+        $menu_title = $menus[$menu_name];
+        $trail_raw = _menu_titles($menu_name, $node->nid);
+        $trail = array();
+        foreach ($trail_raw as $title) {
+          $trail[] = check_plain($title);
+        }
+        
+        // pop the link title off the end
+        $link_title = array_pop($trail);
+        $link_title_raw = array_pop($trail_raw);
+    
+        $values['menupath']            = implode('/', $trail);
+        $values['menupath-raw']        = implode('/', $trail_raw);
+        $values['menu']                = check_plain($menu_title);
+        $values['menu-raw']            = $menu_title;
+        $values['menu-link-title']     = $link_title;
+        $values['menu-link-title-raw'] = $link_title_raw;
       }
       else {
-        $values['menu']         = '';
-        $values['menu-raw']     = '';
-        $values['menupath']     = '';
-        $values['menupath-raw'] = '';
+        $values['menu']                = '';
+        $values['menu-raw']            = '';
+        $values['menupath']            = '';
+        $values['menupath-raw']        = '';
+        $values['menu-link-title']     = $link_title;
+        $values['menu-link-title-raw'] = $link_title_raw;
       }
 
       // And now taxonomy, which is a bit more work. This code is adapted from
@@ -146,6 +145,33 @@ function node_token_values($type, $objec
   return $values;
 }
 
+
+/**
+* Helper function to extract the titles between $nid and the root in a particular menu
+*/
+function _menu_titles($menu_name, $nid) {
+  // get the plid of our target node
+  $item = db_fetch_array(db_query("SELECT mlid, plid, link_title, menu_name FROM {menu_links} WHERE menu_name = '%s' AND link_path = '%s'", $menu_name, 'node/' . $nid));
+  
+  if(!(bool)$item['mlid']){
+    // not in this menu, or the item was just dissasociated
+    return array();
+  }
+  
+  $titles = array();
+  $titles[] = $item['link_title']; // include the node link title as well
+  while($item['plid']){
+    $item = db_fetch_array(db_query("SELECT mlid, plid, link_title FROM {menu_links} WHERE mlid = %d", $item['plid']));
+    $titles[] = $item['link_title'];
+  }
+  
+  // invert the array to return it to path order
+  $titles = array_reverse($titles);
+  
+  return $titles;
+}
+
+
 /**
  * Implementation of hook_token_list()
  */
@@ -185,10 +211,12 @@ function node_token_list($type = 'all') 
     $tokens['node']['d']              = t("Node creation day (one or two digit)");
     $tokens['node']['mod-????']       = t('All tokens for node creation dates can also be used with with the "mod-" prefix; doing so will use the modification date rather than the creation date.');
 
-    $tokens['node']['menu']           = t("The name of the menu the node belongs to.");
-    $tokens['node']['menu-raw']       = t("The name of the menu the node belongs to. WARNING - raw user input.");
-    $tokens['node']['menupath']       = t("The menu path (as reflected in the breadcrumb), not including Home or [menu]. Separated by /.");
-    $tokens['node']['menupath-raw']   = t("The unfiltered menu path (as reflected in the breadcrumb), not including Home or [menu]. Separated by /. WARNING - raw user input.");
+    $tokens['node']['menu']                = t("The name of the menu the node belongs to.");
+    $tokens['node']['menu-raw']            = t("The name of the menu the node belongs to. WARNING - raw user input.");
+    $tokens['node']['menupath']            = t("The menu path (as reflected in the breadcrumb), not including Home or [menu]. Separated by /.");
+    $tokens['node']['menupath-raw']        = t("The unfiltered menu path (as reflected in the breadcrumb), not including Home or [menu]. Separated by /. WARNING - raw user input.");
+    $tokens['node']['menu-link-title']     = t("The text used in the menu as link text for this item.");
+    $tokens['node']['menu-link-title-raw'] = t("The unfiltered text used in the menu as link text for this item. WARNING - raw user input.");
     return $tokens;
   }
 }
