我有一个节点,我想要它的菜单。据我所知,node_load不包含它。显然,根据路径node/nid
编写查询来查找它是微不足道的,但有没有Drupal方法可以做到这一点?
答案 0 :(得分:6)
如果菜单树有多个级别,sql似乎是更好的选择。 下面给出了drupal 7的示例,其中path类似于'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;
}
答案 1 :(得分:3)
Menu Node module公开API来执行此操作。
您可以在代码中阅读文档(Doxygen)。我认为您需要的功能由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.
*/
返回mlid => menu object
的关联数组。你可能只需要第一个,所以看起来像这样:
$arr = menu_node_get_links(123);
list($mlid) = array_keys($arr);
否则,您可以在thread in the Drupal Forums:
中试用该建议使用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;
}