开发者

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

I've got a node, I w开发者_运维问答ant 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?


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


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜