有没有一种方法(sql除外)来获取didpal中给定nid的mlid?
问题描述:
我有一个节点,我想它的菜单。据我所知,node_load不包括它。显然,编写一个查询来查找基于路径node/nid
的查询是微不足道的,但有没有Drupal的方法来做到这一点?有没有一种方法(sql除外)来获取didpal中给定nid的mlid?
答
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;
}
答
如果菜单树有多个级别的sql似乎是一个更好的选择。 一个为Drupal 7样品给出波纹管,其中路径是一样的东西“节点/ 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
辉煌,谢谢。 – 2013-12-05 07:49:25
看起来像会做到这一点,但我认为SQL是更容易...谢谢,虽然。 – sprugman 2010-05-18 19:03:00
没问题。确实,SQL可能更容易。 :) – sirhc 2010-05-18 19:08:48