我正在为我的公司构建一个相当复杂的模块,其中包含许多不同的配置页面。我希望在顶部的管理栏中有一个包含所有子菜单项的菜单项。我知道如何通过UI将单个项目添加到该菜单中,但是会有足够多的页面我更喜欢通过模块本身来完成。那么,如何在我的管理菜单中添加一个带有子菜单的项目,以及“Dashboard'内容'内容'结构'等等模块文件。我认为它必须在hook_menu()中,但我无法弄明白。
答案 0 :(得分:11)
这可以通过在'page callback'
实施中添加system_admin_menu_block_page
hook_menu
来实现:
所以,假设您想要创建如下结构:
- 自定义主菜单(将出现在工具栏上,除 Structure , Modules 等其他项目外)
- 子菜单项1
- 子菜单项2
钩子实现类似于:
function MODULE_menu() {
$items['admin/main'] = array(
'title' => 'Custom main menu',
'description' => 'Main menu item which should appear on the toolbar',
'position' => 'left',
'weight' => -100, // Less weight so that it will appear to the extreme left, before dashboard.
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer site configuration'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/main/sub-menu-1'] = array(
'title' => 'Sub menu item 1',
'description' => 'Child of the menu appearing in toolbar.',
'page callback' => 'drupal_get_form',
'page arguments' => array('custom_form'),
'access arguments' => array('custom permission'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/main/sub-menu-2'] = array(
'title' => 'Sub menu item 2',
'description' => 'Child of the menu appearing in toolbar.',
'page callback' => 'custom_page_callback',
'access arguments' => array('custom permission'),
'type' => MENU_NORMAL_ITEM,
);
}
P.S - 启用模块或将此代码添加到hook_menu实现后,您必须刷新缓存,以便Drupal选择新的菜单结构。