diff -rup token/token_node.inc token_menupath/token_node.inc --- token/token_node.inc Wed Nov 7 12:16:44 2007 +++ token_menupath/token_node.inc Tue Apr 29 16:25:38 2008 @@ -47,32 +47,34 @@ function node_token_values($type, $objec $values['mod-d'] = date('j', $node->changed); // 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]) { - 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'] = array_shift($trail); + 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['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 @@ -123,6 +125,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() */ @@ -165,6 +194,8 @@ function node_token_list($type = 'all') $tokens['node']['menu'] = t("The name of the menu the node belongs to."); $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; }