Drupal菜单系统 - 输出树一级 [英] Drupal Menu System - Outputting a Tree One Level Down

查看:128
本文介绍了Drupal菜单系统 - 输出树一级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读Drupal中的各种菜单功能,但是有很多很多,我已经达到了一个完全混乱和绝望的地步...希望在这里的一个smarties可以帮助我...



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



所以,菜单如下所示:LEVEL ONE> Sublevel A> Sublevel I> Sublevel a



I '尝试以Sublevel A
(即,Sublevel A> Sublevel I> Sublevel a)



开始输出菜单树但是,不能为我的生活找出如何做到这一点...我试着简单地得到Sublevel A菜单(在这种情况下为69),然后

 <?php print theme_menu_tree(69); ?> 

,但只打印出'69'。没有什么我预期的...



任何人都知道如何做到这一点?

解决方案

我一直想知道为什么在核心中没有这个功能,但是afaik没有。



所以看起来我们需要自己滚动,走完一个完整的菜单树,直到找到我们需要的子树:

 / ** 
*根据菜单链接ID(mlid)从菜单树中提取特定的子树
*
* @param array $ tree
* menu_tree_all_data()或menu_tree_page_data()返回的菜单树数据结构
* @param int $ mlid
*返回子树的菜单项的菜单链接ID
* @return array
*找到的子树,如果没有条目与mlid匹配,则为NULL NULL
* /
函数yourModule_menu_get_subtree($ tree,$ mlid){
//检查所有顶部级别条目
foreach($ tree as $ key => $ element){
//这是我们正在寻找的条目?
if($ mlid == $ element ['link'] ['mlid']){
//是,保留键
返回数组($ key => $元件);
}
else {
//否,递归给孩子,如果有
if($ element ['below']){
$ submatch = yourModule_menu_get_subtree($ element ['below'],$ mlid);
//找到孩子中想要的条目?
if($ submatch){
//是的,返回并停止再查找
return $ submatch;
}
}
}
}
//根本没有匹配
return NULL;
}

要使用它,您首先需要获取整个菜单的树,使用 menu_tree_page_data() menu_tree_all_data() ,具体取决于您需要的内容(检查API定义的差异)。然后根据mlid提取你想要的子树。然后可以通过 menu_tree_output()

  $ mlid = 123; // TODO:用逻辑替换来确定需要的mlid 
$ tree = menu_tree_page_data('navigation'); // TODO:将'navigation'替换为您感兴趣的菜单名称
//提取子树
$ subtree = yourModule_menu_get_subtree($ tree,$ mlid);
//渲染为HTML菜单列表
$ submenu = menu_tree_output($ subtree);






免责声明:我不知道这是否是一个很好的/正确的方法 - 这只是通过与OP相同的过程,我想出的解决方案,就是阅读整个菜单模块的功能,总是想知道我是否 m缺少明显的某处...


I've been reading through the various menu functions in Drupal, but there are sooo many, and I've reached a point of utter confusion and despair... Hoping one of the smarties here can help me out...

Basically, I have four levels to my menu. I'm trying to create a tree that outputs from the second level down.

So, the menu looks like this: LEVEL ONE > Sublevel A > Sublevel I > Sublevel a

I'm trying to output the menu tree beginning with Sublevel A (i.e., Sublevel A > Sublevel I > Sublevel a)

But, can't for the life of me figure out how to do that... I tried simply getting the mlid of the Sublevel A menu (in this case 69), and then

<?php print theme_menu_tree(69); ?>

but it just prints out '69'. Not at all what I expected...

Anyone know how to do this?

解决方案

I always wondered why there is no function for this in core, but afaik there is none.

So it looks like we need to roll our own, walking a complete menu tree until we find the subtree we need:

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

To use it, you first need to get the tree for the whole menu, using menu_tree_page_data() or menu_tree_all_data(), depending on what you need (check the API definitions for the difference). Then you extract the subtree you want, based on the mlid. This subtree can then be rendered into HTML via menu_tree_output():

$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);


Disclaimer: I am not sure if this is a good/proper way to do it - it is just the solution I came up with after going through the same procedure as the OP, that is, reading through the whole menu module functions, always wondering if I'm missing the obvious somewhere...

这篇关于Drupal菜单系统 - 输出树一级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆