tags:

views:

85

answers:

1

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?

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
looks like that'll do it, but I think sql is easier... Thanks, though.
sprugman
No problem. Indeed SQL might be easier. :)
sirhc

related questions