Drupal 7:以编程方式将项目添加到管理工具栏/菜单

Lon*_*fPR 4 drupal drupal-7 hook-menu drupal-modules

我正在为我的公司构建一个相当复杂的模块,其中包含许多不同的配置页面.我希望在顶部的管理栏中有一个菜单项,它包含所有子菜单项.我知道如何通过UI将单个项目添加到该菜单中,但是我会更喜欢通过模块本身来完成它.那么,如何在我的模块文件的管理菜单中添加一个带有子菜单的项目,与"仪表板","内容","结构"等坐在一起.我以为它必须在hook_menu()中,但我无法弄明白.

Aji*_*t S 11

这可以通过增加一个可以实现'page callback'system_admin_menu_block_pagehook_menu实现:
所以,让我们说,你想创建一个像下面这样的结构:

  • 自定义主菜单(将出现在工具栏上,除了结构,模块等其他项目)
    • 子菜单项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,
  );
}
Run Code Online (Sandbox Code Playgroud)

PS - 启用模块或将此代码添加到hook_menu实现后,您必须刷新缓存,以便Drupal选择新的菜单结构.