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

I have a node, I want this menu. As far as I can tell, node_load does not include it. Obviously, it is trivial to write a query to find it based on the path node/nid, but is there a Drupal way to do this?

+5
source share
2 answers

The Node menu module provides an API for this. You can read the documentation (Doxygen) in code. I think the necessary functionality is provided by the method menu_node_get_links($nid, $router = FALSE):

/**
 * 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 is returned mlid => menu object. You probably only need the first one to look something like this:

$arr = menu_node_get_links(123);
list($mlid) = array_keys($arr);

Drupal:

node/[nid] $path :

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;
}
+3

sql, . drupal 7 , - - 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;
}
+6

All Articles