【问题标题】:Drupal 7: Add Item to Admin Toolbar/Menu ProgrammaticallyDrupal 7:以编程方式将项目添加到管理工具栏/菜单
【发布时间】:2014-05-19 19:25:31
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    这可以通过将system_admin_menu_block_page'page callback' 添加到您的hook_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,
      );
    }
    

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

    【讨论】:

    • 这很好用。作为说明,我不需要system_admin_menu_block_page。我可以将'page callback' 设置为任何东西。我认为是 positionweightfilefile path 参数做到了。
    猜你喜欢
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    相关资源
    最近更新 更多