Drupal模块嵌套菜单项 [英] Drupal module nested menu items

查看:74
本文介绍了Drupal模块嵌套菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为模块实现 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
        • MenuItem1
        • SubMenu
          • SubMenuItem1

          但是,所有项目都显示在MyModule菜单下.

          However, all items appear under the MyModule menu.

          • 我的模块
            • MenuItem1
            • SubMenuItem1
            • 子菜单
            • MyModule
              • MenuItem1
              • SubMenuItem1
              • SubMenu

              我在做什么错了?

              * 错字(我已经修复)导致SubMenu是一个单独的元素,而不是MyModule的子元素.我仍然不明白为什么SubMenuItem1不能在SubMenu下呈现.

              * A typo (which I have fixed) caused SubMenu to be a separate element rather than a child element of MyModule. I still don't understand why SubMenuItem1 does not render under the SubMenu, 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()定义的path <>回调关系. 这并没有定义任何真实的"菜单项(例如在菜单菜单中,例如导航菜单).它仅定义了Drupal内部菜单结构,与显示的菜单无关.仅具有将路径映射到回调函数的内部层次结构!

              Drupal 6 split the menu definition storage in two tables. There is the menu_router table, which stores the path<>callback relations defined via hook_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"(拼合)来定义嵌套顺序,该父条目指向父条目,而对于顶层条目则为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_ITEMMENU_SUGGESTED_ITEM,Drupal将另外尝试在menu_links表中创建一个条目. 如果该路径的条目已经存在,Drupal不会更改其在层次结构中的位置,因为它假定用户是故意移动它的.您应该考虑使用hook_menu()创建此menu_link条目>为方便起见,可以通过下面提到的功能显式添加它们,从而省去了麻烦,但是该机制不是很灵活,并且尝试不干扰现有配置(否则,每次重新构建时,手动编辑的菜单都会不断地重新排序)菜单缓存).

              Now whenever you define path/callback combinations via hook_menu(), Drupal just puts that entry into the menu_router table. If you define them as MENU_NORMAL_ITEM or MENU_SUGGESTED_ITEM, Drupal will additionally try to create an entry in the menu_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 this menu_link entry creation by hook_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_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() and menu_link_maintain() functions. You might also want to read When and how to use menu_links.

              这篇关于Drupal模块嵌套菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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