Drupal 7: add item to admin toolbar / menu programmatically

I am creating a pretty attractive module for my company with several configuration pages. I would like there to be a menu item in the admin panel at the top, which also has all the submenu items. I know how to add one item to this menu through the user interface, but there will be enough pages that I would prefer to do using the module itself. So, how can I add an item with a submenu to sit next to "Dashboard", "Content", "Structure", etc. In the admin menu in my module file. I suggested that this should be in hook_menu (), but I can't figure it out.

+4
source share
1 answer

This can be achieved by adding 'page callback'of system_admin_menu_block_pageto your implementation hook_menu:
So, let's say you want to create a structure such as:

  • Custom main menu (appears on the toolbar, among other elements such as Structure, Modules, etc.)
    • Submenu item 1
    • Submenu item 2

The hook implementation will look something like this:

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,
  );
}

PS - After you enable the module or add this code to the hook_menu implementation, you will need to clear the cache so that Drupal builds a new menu structure.

+9
source

Source: https://habr.com/ru/post/1541166/


All Articles