Drupal 7 : Add Item to Admin Toolbar/Menu Programmatically

标签 drupal drupal-7 drupal-modules hook-menu

我正在为我的公司构建一个相当复杂的模块,其中包含许多不同的配置页面。我希望在顶部的管理栏中有一个菜单项,其中包含所有子菜单项。我知道如何通过 UI 将单个项目添加到该菜单,但是会有足够多的页面我宁愿通过模块本身来完成。那么,如何在我的模块文件的管理菜单中添加带有子菜单的项目以与“仪表板”、“内容”、“结构”等并排放置。我认为它必须在 hook_menu() 中,但我无法弄清楚。

最佳答案

这可以通过添加 'page callback' 来实现的 system_admin_menu_block_page给您的hook_menu执行:
因此,假设您要创建如下结构:

  • Custom main menu (will appear on the toolbar, besides other items like Structure, Modules, etc)
    • Sub menu item 1
    • Sub menu item 2


钩子(Hook)实现将类似于:
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 选择新的菜单结构。

关于Drupal 7 : Add Item to Admin Toolbar/Menu Programmatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23745673/

相关文章:

html - 内联 CSS 以创建一行水平图像

php - Drupal负载平衡

mysql - Drupal编码和节点插入

php - 模块可以下载文件吗?

drupal - drupal View 中的自定义查询 - 现在无法排序

html - 用于在 drupal 上包含 HTML5 视频播放器和流行平台内容的字段

php - db_select 向我发送所有值,而不是我想要的值

html - 移动文本区域而不是调整文本大小

Drupal Media + Wysiwyg (+CKEditor) 插入图像问题

php - 关闭 Drupal 7 的缓存