I am currently working on a module that finds out the parent menu item by url path then displays the current tree structure of that related menu by finding the top parent and most important only displaying that menu item and submenus from within that menu.
我目前正在开发一个模块,该模块通过url路径查找父菜单项,然后通过查找父菜单和最重要的只显示菜单项和子菜单来显示相关菜单的当前树结构。
A simple solution would be having a foreach loop thru all items or an array with all items as keys.
一个简单的解决方案是通过所有项创建一个foreach循环,或者使用所有项作为键的数组。
path = '/system/menu/submenu';
parent = 'system';
output = parent + parent submenus.
All menu items in "Custom menu":
“自定义菜单”中的所有菜单项:
- System
- Menu wrapper
- SubMenu 1A
- SubMenu 2A
- SubMenu 3A
- Main
- SubMenu 1B
- SubMenu 2B
- SubChildMenu 3B
PHP code should return this:
PHP代码应该返回:
- System
- Menu wrapper
- SubMenu 1A
- SubMenu 2A
- SubMenu 3A
My code (currently not working):
我的代码(目前不工作):
$menu = menu_tree('my-custom-menu');
return = theme('my_custom_menu', array('system' => $menu_tree));
Notice: This needs to be php code and php code only, all other modules does not support this only thru manual selection. The content is displayed thru a block.
注意:这只需要php代码和php代码,所有其他模块都不支持这只通过手动选择。内容通过块显示。
"For the navigation, Drupal will call menu_tree_page_data via menu_navigation_links which will only return a single level of links. You can follow this function up to menu_main_menu and then template_preprocess_page before it (which is how it ends up as a variable in page.tpl.php)
对于导航,Drupal将通过menu_navigation_links调用menu_tree_page_data,它只会返回单个级别的链接。您可以跟踪这个函数,直到menu_main_menu,然后是template_preprocess_page(这就是它在page.tpl.php中的变量)
However, if you insert a menu as a block menu_tree_page_data is called by menu_tree (which calls menu_tree_output immediately after which does some additional work to the array for the final markup.) After both have run you have your whole menu tree available as an array which I then ran through some custom PHP code to loop through the array and render it into an HTML list."
但是,如果您插入一个菜单作为一个block menu_tree_page_data被menu_tree调用(它立即调用menu_tree_output,然后对数组做一些额外的工作,以完成最终标记)。在这两个都运行之后,你可以将整个菜单树作为一个数组来使用,然后我通过一些自定义PHP代码对数组进行循环,并将其呈现为一个HTML列表。
People have asked this before but i've tried their solutions and no success probable cause would be those questions is for drupal 6. I'm currently working with drupal 7.
人们以前问过这个问题,但我尝试过他们的解决方案,没有成功的可能原因是这些问题是针对drupal 6的。我目前正在与drupal7合作。
Your help is very much appreciated, thanks...
非常感谢您的帮助,谢谢……
Related questions:
相关问题:
Restrict menu tree to first level
将菜单树限制为一级
how to get all the menu items below a certain parent in drupal?
如何在drupal中获取某个父元素下面的所有菜单项?
https://drupal.stackexchange.com/questions/28654/how-to-display-submenus-separate-from-their-menu-tree-in-drupal-7
https://drupal.stackexchange.com/questions/30112/displaying-a-menus-child-links
https://drupal.stackexchange.com/questions/30112/displaying-a-menus-child-links
2 个解决方案
#1
2
First thing you need is the mlid of the page your currently on:
首先你需要的是你当前页面的mlid:
$q = variable_get('site_frontpage', 'node') == $_GET["q"] ? '<front>' : $_GET["q"];
$current_menu_item = db_select('menu_links' , 'ml')
->condition('ml.link_path' , $q)
->fields('ml', array('mlid', 'plid'))
->execute()
->fetchAll();
Now if the plid of this menu item is 0 then we know it is a top level menu item, if it is not 0 then we need to get the parent. So the top mlid of the tree we want to get is:
如果这个菜单项的plid是0那么我们知道它是一个*的菜单项,如果它不是0那么我们需要得到父菜单项。所以我们想要得到的树的顶部mlid是:
$top_level_mlid = $current_menu_item->plid == 0 ? $current_menu_item->mlid : $current_menu_item->plid;
Then you need to load your full menu:
然后你需要加载你的全部菜单:
$full_menu_items = menu_tree_all_data('main-menu');
Now loop though all the menu items and just get the bit we want:
现在循环遍历所有的菜单项,得到我们想要的位:
foreach($full_menu_items as $menu_item) {
if($menu_item['link']['mlid'] == $top_level_mlid) {
$links = $menu_item['below'];
break;
}
}
$links now hold all of the links from the specific part of the menu that you are after.
$links现在保存来自您要查找的菜单特定部分的所有链接。
And to output that array as a menu on the page:
并输出该数组作为页面上的菜单:
echo theme('links__system_secondary_menu', array(
'links' => $links,
'attributes' => array(
'id' => 'secondary-menu',
'class' => array('links', 'clearfix')),
'heading' => array(
'text' => t('Secondary menu'),
'level' => 'h2',
'class' => array('element-invisible')
)
));
#2
0
How about something like this?
像这样的东西怎么样?
function your_module_menu_link__your_menu($variables){
$element = $variables['element'];
$sub_menu = '';
if ($element['#below']) {
$sub_menu = drupal_render($element['#below']);
}
$ouput = "";
if($element['#original_link']['plid'] > 0 || $element['#below']){
$link = l($element['#title'], $element['#href'], $element['#localized_options']);
$ouput = '<li' . drupal_attributes($element['#attributes']) . '>' . $link . $sub_menu . "</li>\n";
}
return $ouput;
}
#1
2
First thing you need is the mlid of the page your currently on:
首先你需要的是你当前页面的mlid:
$q = variable_get('site_frontpage', 'node') == $_GET["q"] ? '<front>' : $_GET["q"];
$current_menu_item = db_select('menu_links' , 'ml')
->condition('ml.link_path' , $q)
->fields('ml', array('mlid', 'plid'))
->execute()
->fetchAll();
Now if the plid of this menu item is 0 then we know it is a top level menu item, if it is not 0 then we need to get the parent. So the top mlid of the tree we want to get is:
如果这个菜单项的plid是0那么我们知道它是一个*的菜单项,如果它不是0那么我们需要得到父菜单项。所以我们想要得到的树的顶部mlid是:
$top_level_mlid = $current_menu_item->plid == 0 ? $current_menu_item->mlid : $current_menu_item->plid;
Then you need to load your full menu:
然后你需要加载你的全部菜单:
$full_menu_items = menu_tree_all_data('main-menu');
Now loop though all the menu items and just get the bit we want:
现在循环遍历所有的菜单项,得到我们想要的位:
foreach($full_menu_items as $menu_item) {
if($menu_item['link']['mlid'] == $top_level_mlid) {
$links = $menu_item['below'];
break;
}
}
$links now hold all of the links from the specific part of the menu that you are after.
$links现在保存来自您要查找的菜单特定部分的所有链接。
And to output that array as a menu on the page:
并输出该数组作为页面上的菜单:
echo theme('links__system_secondary_menu', array(
'links' => $links,
'attributes' => array(
'id' => 'secondary-menu',
'class' => array('links', 'clearfix')),
'heading' => array(
'text' => t('Secondary menu'),
'level' => 'h2',
'class' => array('element-invisible')
)
));
#2
0
How about something like this?
像这样的东西怎么样?
function your_module_menu_link__your_menu($variables){
$element = $variables['element'];
$sub_menu = '';
if ($element['#below']) {
$sub_menu = drupal_render($element['#below']);
}
$ouput = "";
if($element['#original_link']['plid'] > 0 || $element['#below']){
$link = l($element['#title'], $element['#href'], $element['#localized_options']);
$ouput = '<li' . drupal_attributes($element['#attributes']) . '>' . $link . $sub_menu . "</li>\n";
}
return $ouput;
}