Drupal菜单系统 - 向下输出一级树

php*_*00b 5 tree drupal menu

我一直在阅读Drupal中的各种菜单功能,但是太多了,我已经达到了完全混乱和绝望的程度......希望这里的一个聪明人可以帮助我......

基本上,我的菜单有四个级别.我正在尝试创建一个从第二级输出的树.

所以,菜单看起来像这样:第一级>分段A>分段I>分段a

我正在尝试输出以Sublevel A开头的菜单树(即,Sublevel A> Sublevel I> Sublevel a)

但是,不能为我的生活弄清楚如何做到这一点...我试着简单地获得Sublevel A菜单的mlid(在这种情况下为69),然后

<?php print theme_menu_tree(69); ?>
Run Code Online (Sandbox Code Playgroud)

但它打印出'69'.完全不是我所期待的......

有人知道怎么做吗?

jhe*_*rom 13

菜单块模块会做正是你需要的.(它使用与上面提出的自定义函数类似的逻辑).


Hen*_*pel 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;
}
Run Code Online (Sandbox Code Playgroud)

要使用它,首先需要使用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);
Run Code Online (Sandbox Code Playgroud)

免责声明:我不确定这是否是一个好的/正确的方法 - 它只是我在通过与OP相同的程序后想出的解决方案,即通读整个菜单模块的功能,总是在想如果我错过了明显的地方......