我有一个节点,我想要它的菜单.据我所知,node_load不包含它.显然,编写一个基于路径查找它的查询是微不足道的node/nid,但有没有Drupal方法呢?
如果菜单树有多个级别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;
}
Run Code Online (Sandbox Code Playgroud)
菜单节点模块公开了一个 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.
*/
Run Code Online (Sandbox Code Playgroud)
mlid => menu object返回一个关联数组。您可能只需要第一个,因此它可能看起来像这样:
$arr = menu_node_get_links(123);
list($mlid) = array_keys($arr);
Run Code Online (Sandbox Code Playgroud)
否则,您可以尝试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;
}
Run Code Online (Sandbox Code Playgroud)