2009-12-03 14 views
5

私はDrupalのさまざまなメニュー機能を読んできましたが、たくさんのsoooがあり、私は完全な混乱と絶望の点に達しました...ここのスマートは私を助けることができます...Drupal Menu System - 1レベル下にツリーを出力する

基本的に、私は私のメニューに4つのレベルがあります。私は2番目のレベルから出力するツリーを作成しようとしています。

ので、メニューは次のようになります。LEVEL ONE>サブレベルA>サブレベルI>サブレベル

私は出力にサブレベルAで始まるメニューツリーをしようとしている (すなわち、サブレベルA>サブレベルI>サブレベルA)

しかし、私の人生のために...私は単に

<?php print theme_menu_tree(69); ?> 
この場合69)に(サブレ​​ベルAメニューのMLIDを取得し、しようとしたことを行う方法を見つけ出すことはできません

しかし、それは単に '69'を出力します。私はまったく期待していなかった...

誰でもこれを行う方法を知っていますか?

+1

テーマ関数を直接呼び出すことはできません。 <?php print theme( 'menu_tree'、69); ?> は、予期しない結果を得るための正しい方法でした:) – Rimian

答えて

11

私はいつもコアでこれが機能していないのはなぜだろうと思っていましたが、そこには誰もいません。

私たちは私たち自身をロールバックする必要があるようなので、それは私たちが必要とする部分木を見つけるまで、完全なメニューツリーを歩いて、になります。これを使用するには

/** 
* 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

+0

サブメニューの出力をテーマにするにはどうすればよいですか? –

+0

@matt ryan: 'menu_tree_output()'はすでにテーマメニューを返していますので、何を意味するのか分かりません。出力の作成方法に影響を与えたい場合は、関数のリンクされたAPIドキュメントページを見てください。これには 'theme_menu_item_link'、' theme_menu_item'および 'theme_menu_tree'が使われます。 –

13

Menu Blockモジュールは、 (これは上記のカスタム関数と同様のロジックを使用します)。

+1

+1 - 良いキャッチ。 OPのコードを保存することもあります:) –

+0

+1 - 本当に素敵な人! :) – mac

1

まだカスタム機能のパス...今日 - 全く異なるものを探している理由 - 同じ問題に直面しているもう1人の同僚が見つかりました。

元の投稿はhereです。以下は、コードスニペットのc & pです。

// 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); 
    } 
} 
関連する問題