Drupal 模块嵌套菜单项 [英] Drupal module nested menu items
问题描述
在为模块实现 hook_menu 时,我试图将一些项目放入一个子菜单.
In implementing hook_menu for a module, I am trying to put some items into a submenu.
到目前为止我有这样的东西
So far I have something like this
$items['MyModule'] = array(
//...
'page callback' => 'system_admin_menu_block_page',
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module','system'),
);
$items['MyModule/MenuItem1'] = array(
//...
);
$items['MyModule/SubMenu'] = array(
//...
'page callback' => 'system_admin_menu_block_page',
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module','system'),
);
$items['MyModule/SubMenu/SubMenuItem1'] = array(
//...
);
我希望 SubMenu
显示为 MyModule
菜单的子菜单,并且 SubMenuItems
显示在该子菜单下.这是 Drupal API 文档中描述的默认行为.
I expect the SubMenu
to appear as, well, a submenu to the MyModule
menu, and for the SubMenuItems
to appear under that submenu. This is the default behaviour described at the Drupal API documentation.
- 我的模块
- MenuItem1
- 子菜单
- SubMenuItem1
但是,所有项目都显示在
MyModule
菜单下.However, all items appear under the
MyModule
menu.- 我的模块
- MenuItem1
- SubMenuItem1
- 子菜单
我做错了什么?
*拼写错误(我已修复)导致
SubMenu
成为一个单独的元素,而不是MyModule
的子元素.我仍然不明白为什么SubMenuItem1
没有在SubMenu
下呈现.* A typo (which I have fixed) caused
SubMenu
to be a separate element rather than a child element ofMyModule
. I still don't understand whySubMenuItem1
does not render under theSubMenu
, though.推荐答案
我无法重现您的问题 - 使用您的菜单层次结构,所有条目都以预期的顺序和嵌套出现在导航菜单下.
I can not reproduce your problem - using your menu hierarchy, all entries appear under the navigation menu in the expected order and nesting.
您是否(重新)尝试从干净的状态(即,您的模块已卸载且菜单项已消失)?为了解释我问这个的原因,我必须详细说明一下:
Have you (re)tried from a clean state (that is, with your module uninstalled and the menu entries gone)? To explain why I ask this, I have to elaborate a bit:
Drupal 6 将菜单定义存储拆分为两个表.有
menu_router
表,它存储了通过hook_menu()
定义的路径<>回调关系.这并没有定义任何真正的"菜单条目(如在菜单菜单中,例如导航菜单).它只定义了 Drupal 内部菜单结构,与显示的菜单无关,但是仅具有映射路径到回调函数的内部层次结构!Drupal 6 split the menu definition storage in two tables. There is the
menu_router
table, which stores the path<>callback relations defined viahook_menu()
. This does not define any 'real' menu entry (as in menu menu, e.g. the navigation menu). It does only define the Drupal internal menu structure, which has nothing to do with the displayed menus, but only with the internal hierarchy of mapping paths to callback functions!然后是
menu_links
表,它存储出现在各种可显示菜单(例如导航、主链接等)下的真实"菜单条目.那里的条目还通过为每个条目存储一个父菜单 ID"(plid)来定义嵌套顺序,指向父条目,或者为顶级条目存储 0.Then there is the
menu_links
table, which stores the 'real' menu entries as they appear under the various displayable menus (e.g. navigation, primary links, etc.). The entries there also define the nesting order by storing a 'parent menu id' (plid) for each entry, pointing to the parent entry, or 0 for a top level entry.现在,每当您通过
hook_menu()
定义路径/回调组合时,Drupal 都会将该条目放入menu_router
表中.如果您将它们定义为MENU_NORMAL_ITEM
或MENU_SUGGESTED_ITEM
,Drupal 将另外尝试在menu_links
表中创建一个条目.如果该路径的条目已经存在,Drupal 不会改变它在层次结构中的位置,因为它假定用户是故意移动它的.你应该考虑这个menu_link
hook_menu()
创建条目作为一个方便的添加项,可以省去您通过下面提到的函数显式添加它们的麻烦,但该机制不是很灵活,并且尽量不干扰现有配置(否则手动编辑的菜单会在每次重建菜单缓存时不断重新排序).Now whenever you define path/callback combinations via
hook_menu()
, Drupal just puts that entry into themenu_router
table. If you define them asMENU_NORMAL_ITEM
orMENU_SUGGESTED_ITEM
, Drupal will additionally try to create an entry in themenu_links
table. If an entry for that path already exists, Drupal will not alter its placement in the hierarchy, as it assumes that a user moved it on purpose. You should think of thismenu_link
entry creation byhook_menu()
as a convenience add on that can save you the trouble off explicitly adding them via the functions mentioned below, but the mechanism is not very flexible and tries not to interfere with existing configurations (otherwise a manually edited menu would constantly get reordered on every rebuilding of the menu cache).所以你应该再试一次,同时确保你的路径在 `menu_links' 表中没有现有的条目.
So you should try again while making sure that none of your paths have an existing entry in the `menu_links' table.
为了在安装模块时提供适当的默认菜单(并更好地控制发生的事情),您应该查看
menu_link_save()
和menu_link_maintain()
函数.您可能还想阅读何时以及如何使用 menu_links.For your goal of providing a proper default menu on install of your module (and for more control over whats happening), you should take a look into the
menu_link_save()
andmenu_link_maintain()
functions. You might also want to read When and how to use menu_links.这篇关于Drupal 模块嵌套菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!