2009-12-03 12 views
5

我一直在閱讀Drupal中的各種菜單功能,但有很多很多,我已經達到了一個完全混亂和絕望的地步...希望其中一個這裏的聰明人可以幫助我...Drupal菜單系統 - 輸出樹一級關閉

基本上,我有四個級別菜單。我試圖創建一個從第二層輸出的樹。

因此,菜單如下:LEVEL ONE>綜A>綜I>綜一個

我試圖輸出與綜A開頭的菜單樹 (即無底柱分段A>綜I>綜一)

但是,不能爲我的生活弄清楚如何做到這一點...我試圖僅僅讓無底柱分段菜單的MLID(在這種情況下69),然後

<?php print theme_menu_tree(69); ?> 

但它只打印出'69'。根本不是我所期望的...

任何人都知道如何做到這一點?

+1

您不應該直接調用主題函數。 <?php print theme('menu_tree',69); ?> 本來是得到意想不到的結果的正確方法:) – Rimian 2009-12-06 04:21:54

回答

11

我總是想知道爲什麼在覈心中沒有這個功能,但afaik沒有。

所以看起來我們需要推出我們自己的,走一個完整的菜單樹,直到我們找到我們需要的子樹:

/** 
* Extract a specific subtree from a menu tree based on a menu link id (mlid) 
* 
* @param array $tree 
* A menu tree data structure as returned by menu_tree_all_data() or menu_tree_page_data() 
* @param int $mlid 
* The menu link id of the menu entry for which to return the subtree 
* @return array 
* The found subtree, or NULL if no entry matched the mlid 
*/ 
function yourModule_menu_get_subtree($tree, $mlid) { 
    // Check all top level entries 
    foreach ($tree as $key => $element) { 
    // Is this the entry we are looking for? 
    if ($mlid == $element['link']['mlid']) { 
     // Yes, return while keeping the key 
     return array($key => $element); 
    } 
    else { 
     // No, recurse to children, if any 
     if ($element['below']) { 
     $submatch = yourModule_menu_get_subtree($element['below'], $mlid); 
     // Found wanted entry within the children? 
     if ($submatch) { 
      // Yes, return it and stop looking any further 
      return $submatch; 
     } 
     } 
    } 
    } 
    // No match at all 
    return NULL; 
} 

要使用它,你首先需要獲得樹的整個菜單,使用menu_tree_page_data()menu_tree_all_data(),這取決於你需要什麼(檢查差異的API定義)。然後根據mlid提取你想要的子樹。此子樹然後可以通過menu_tree_output()呈現爲HTML:

$mlid = 123; // TODO: Replace with logic to determine wanted mlid 
$tree = menu_tree_page_data('navigation'); // TODO: Replace 'navigation' with name of menu you're interested in 
// Extract subtree 
$subtree = yourModule_menu_get_subtree($tree, $mlid); 
// Render as HTML menu list 
$submenu = menu_tree_output($subtree); 

免責聲明:我不知道這是做一個好/有道 - 這就是我想出了一個解決方案在經歷與OP相同的過程之後,即通過閱讀整個菜單模塊的功能,總是想知道我是否錯過了明顯的某處...

+0

當我今天早些時候看到這個問題時,我決定不回答,因爲我本來能夠給出的唯一答案就是沿着這些路線,似乎是錯誤的方法。現在你發佈了你的答案,我不知道我是否必須開心,因爲我得出的結論與你的結論相同,或者不愉快,因爲沒有更好的方法來做到這一點。總之:+1。 – mac 2009-12-03 22:55:42

+0

主題子菜單輸出的最佳方式是什麼? – 2010-09-28 15:27:38

+0

@matt ryan:'menu_tree_output()'已經返回一個主題菜單,所以我不確定你的意思。如果您想影響它創建輸出的方式,請查看該函數的鏈接API文檔頁面 - 它使用'theme_menu_item_link','theme_menu_item'和'theme_menu_tree'來實現此功能。 – 2010-09-28 15:56:01

13

Menu Block模塊將完全按照您的需要進行操作。 (它使用與上述自定義函數類似的邏輯)。

+1

+1 - 良好的捕獲。可能很好地保存OP一些編碼:) – 2009-12-04 00:06:29

+0

+1 - 確實很不錯! :) – mac 2009-12-04 18:49:46

1

仍然在自定義功能的路徑上...今天 - 爲什麼尋找完全不同的東西 - 我發現另一個同事面臨同樣的問題,並提出了另一個解決方案。

原帖是here。以下是該代碼段中的代碼段。

// will return all menu items under "administration". 
print theme('menu_tree_by_path','admin'); 

// will return links to all node submission forms 
print theme('menu_tree_by_path','node/add'); 

// return the correct menu array by path 
function menu_get_mid_by_path($path) { 
// oddly, menu_get_item accepts a path, but returns the parent id. 
    $menu = menu_get_item(null, $path); 
    if (isset($menu['children'])) { 
// so we have to extract the mid for theme_menu_tree from one of the child items 
    if ($pid = end($menu['children'])) { 
     $menu = menu_get_item($pid); 
     return $menu['pid']; 
    } 
    } 
} 

//theme the crap out of it 
function theme_menu_tree_by_path($path) { 
    if ($mid = menu_get_mid_by_path($path)) { 
    return theme('menu_tree', $mid); 
    } 
}