2010-05-13 43 views

回答

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

看起來像會做到這一點,但我認爲SQL是更容易...謝謝,雖然。 – sprugman 2010-05-18 19:03:00

+0

沒問題。確實,SQL可能更容易。 :) – sirhc 2010-05-18 19:08:48

6

如果菜單樹有多個級別的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

相關問題