Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way (other than sql) to get the mlid for a given nid in drupal?

Tags:

menu

drupal

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?

like image 356
sprugman Avatar asked May 13 '10 18:05

sprugman


2 Answers

if the menu tree has multiple levels sql seems a better option. a sample for drupal 7 is given bellow where path is something like 'node/x'

function _get_mlid($path, $menu_name) {

$mlid = db_select('menu_links' , 'ml')
->condition('ml.link_path' , $path)
->condition('ml.menu_name',$menu_name)
->fields('ml' , array('mlid'))
->execute()
->fetchField();
return $mlid;
}
like image 177
Nikos Tsagkas Avatar answered Nov 16 '22 15:11

Nikos Tsagkas


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;
}
like image 26
sirhc Avatar answered Nov 16 '22 15:11

sirhc