Last updated June 10, 2009. Created on September 7, 2006.
Edited by jhodgdon, authentictech, pwolanin, RobRoy. Log in to edit this page.

Main topic described: Using Drupal Menu system
Drupal hook described: hook_menu

As mentioned previously, the function we just wrote isn't a 'hook': it's not a Drupal recognized name. We need to tell Drupal how to access the function when displaying a page. We do this with Drupal's hook_menu. Remember that we have already used hook_menu in here. The hook_menu defines the association between a URL and the function that creates the content for that url. The hook also does permission checking, if desired. We will use the hook_menu made earlier and continue with it.

<?php
function onthisdate_menu() {

 
$items = array();

 
//this was created earlier in tutorial 7.
 
$items[] = array(
   
'path' => 'admin/settings/onthisdate',
   
'title' => t('On this date module settings'),
   
'callback' => 'drupal_get_form',
   
'callback arguments' => 'onthisdate_admin',
   
'access' => user_access('access administration pages'),
   
'type' => MENU_NORMAL_ITEM,
   );

 
//this is added for this current tutorial.
 
$items[] = array(
   
'path' => 'onthisdate',
   
'title' => t('on this date'),
   
'callback' => 'onthisdate_all',
   
'access' => user_access('access onthisdate content'),
   
'type' => MENU_CALLBACK
 
);

  return
$items;
}
?>

Basically, we're saying if the user goes to "onthisdate" (either via ?q=onthisdate or http://.../onthisdate), the content generated by onthisdate_all will be displayed. The title of the page will be "on this date". The type MENU_CALLBACK tells Drupal to not display the link in the user's menu, just use this function when the URL is accessed. Use MENU_NORMAL_ITEM if you want the user to see the link in the side navigation block.

More information on the menu system:
Drupal 5.x

As mentioned before, the menu hook can handle permission checking before rendering the page. The 'access' entry in the menu item array is where this check is done. If you added a value in your permissions array in the perm hook function, you can use that string as a parameter in the user_access function. If the user isn't in a role that has that permission, the page will not render for the user.

If the module has not been enabled, enable it. If you have already enabled it, in order to reset the menu definitions in the system, you'll need to disable, then reenable it.

Now, navigate to /onthisdate (or ?q=onthisdate) and see what you get.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

miguel’s picture

To show your created link in an other menu than 'Navigation' you can define pid: 'pid' => MENU_MID (See http://drupal.org/node/10901).

How to find the mid of the desired menu by hand

  • Got to your Menu administration (admin/build/menu)
  • Look for your menu
  • Hover your mouse over the entry 'edit'. You will see a path like /admin/build/menu/menu/edit/39
  • Use the shown number as pid. In this example it would be '39'

How to find the mid of the desired menu with a query

<?php
  $menu_id
= db_fetch_array(db_query("SELECT `mid` FROM {menu} WHERE `title` = '%s'", 'MENU TITLE you are searching the mid'));
?>

Then use 'pid' => $menu_id['mid'].

The modified code from the example

<?php
function onthisdate_menu() {

 
// get mid from a certain Menu
$menu_id = db_fetch_array(db_query("SELECT `mid` FROM {menu} WHERE `title` = '%s'", 'MENU TITLE you are searching the mid' ));

 
$items = array();

 
//this was created earlier in tutorial 7.
 
$items[] = array(
   
'path' => 'admin/settings/onthisdate',
   
'title' => t('On this date module settings'),
   
'callback' => 'drupal_get_form',
   
'callback arguments' => 'onthisdate_admin',
   
'access' => user_access('access administration pages'),
   
'pid' =>  $menu_id['mid'],
   
'description' => t('An additional description for the link title'),
   
'type' => MENU_NORMAL_ITEM,
   );

 
//this is added for this current tutorial.
 
$items[] = array(
   
'path' => 'onthisdate',
   
'title' => t('on this date'),
   
'callback' => 'onthisdate_all',
   
'access' => user_access('access onthisdate content'),
   
'type' => MENU_CALLBACK
 
);

  return
$items;
}
?>
chirale’s picture

If you have already enabled it, in order to reset the menu definitions in the system, you'll need to disable, then reenable it.

You can also try to visit "admin/build/menu" without disabling the module. It works (tested on Drupal 5.10) with added and changed menu entries.