How to add a menu item from my custom Drupal 7-module

12,550

Drupal hooks are based on a naming convention, if the hook is called hook_menu() and your module is called mymodule then the function you need to implement is called mymodule_menu(). The code would look like this: 

function mymodule_menu() {
  $items['user'] = array(
    'title'           => 'Page name',
    'description'     => t('Account'),
    'menu_name'       => 'main-menu',
    'weight'          => 8,
    'access callback' => TRUE,
    'page callback'   => 'drupal_goto',
    'page arguments' => array('user'),
    'access arguments' => array('view own profile'), // permission
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

However, the path user is already declared in the system so what you're doing will create all sorts of problems. What you want to do instead is use hook_menu_alter() and change the menu_name for the already existing path:

function mymodule_menu_alter(&$items) {
  $items['user']['menu_name'] = 'main-menu';
}

Doing it this way you won't need to provide your own page callback as the standard user path will just deliver the normal page. The only difference is that once you've cleared Drupal's caches the link to the account should appear in the main menu.

Share:
12,550
Piezo Pea
Author by

Piezo Pea

Drupal developer.

Updated on June 04, 2022

Comments

  • Piezo Pea
    Piezo Pea almost 2 years

    with my custom drupal-module i'm trying to insert add a menu-item to a menu using hook_menu(). It should display the user-name linked to the user-profile. (My task could be similar to Add menu item/link to Drupal menu (programatically?).)

    [Edit:] I tried to solve the task with the following, but it could be, that its the wrong way to do it..

    function mymodule_view_user_page()
    {  
      global $user;
      if ($user->uid != 0) {
        /*$items = array(
          'link_path'   => drupal_get_normal_path('user'),
          'link_title'  => 'Account',
          'menu_name'   => 'main-menu',
          'weight'      => 8,
        );*/
    
        $items['user'] = array(
          'title'           => 'Page name',
          'description'     => t('Account'),
          'menu_name'       => 'main-menu',
          'weight'          => 8,
          'access callback' => TRUE,
          'page callback'   => 'mymodule_view_user_page',
          'access arguments' => array('view own profile'), // permission
          'type' => MENU_NORMAL_ITEM,
    
        );
        return $items;
      }
    }
    
    function mymodule_view_user_page_view_user_page()
    {
      drupal_goto('user');
    }
    

    With the code above there is nothing showing up, but also no errors..

    I probably don't want to use an page callback since that page already exists properly but i'm unsure about not setting it.

    The hook_menu() documentation-page hook_menu is really extensive but it seems i'm not understanding it correct and beginning to loose hairs over it..

    Thank you for hints! PP

  • Piezo Pea
    Piezo Pea about 12 years
    Thank you for your answer! - Since your solution is only for modifing the menu-item for this path, do you think there is the possibility to add a custom menu item to the menu? Like user/uid/edit..