I've got a node, I want it's menu. As far as I can tell, node_load doesn't include it. Obviously, it's trivial to write a query to find it based on the path node/nid
, but is there a Drupal Way to do it?
views:
85answers:
1
A:
The Menu Node module exposes an API to do this.
You can read the documentation (Doxygen) in the code. I think the functionality you need is provided by the menu_node_get_links($nid, $router = FALSE)
method:
/**
* Get the relevant menu links for a node.
* @param $nid
* The node id.
* @param $router
* Boolean flag indicating whether to attach the menu router item to the $item object.
* If set to TRUE, the router will be set as $item->menu_router.
* @return
* An array of complete menu_link objects or an empy array on failure.
*/
An associative array of mlid => menu object
is returned. You probably only need the first one so it might look like something like this:
$arr = menu_node_get_links(123);
list($mlid) = array_keys($arr);
Otherwise, you can try out the suggestion in a thread in the Drupal Forums:
Use node/[nid]
as the $path argument for:
function _get_mlid($path) {
$mlid = null;
$tree = menu_tree_all_data('primary-links');
foreach($tree as $item) {
if ($item['link']['link_path'] == $path) {
$mlid = $item['link']['mlid'];
break;
}
}
return $mlid;
}
sirhc
2010-05-15 09:15:11
looks like that'll do it, but I think sql is easier... Thanks, though.
sprugman
2010-05-18 19:03:00
No problem. Indeed SQL might be easier. :)
sirhc
2010-05-18 19:08:48