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

We've just created a function that will generate a page with links to the content created on a particular day. If we want anyone to see that page, the next thing we have to do is to assign it a URL, via Drupal's hook_menu(). We have already used hook_menu to define the URL for the settings page, so we'll just need to add another entry to that function:


function onthisdate_menu() {

  $items = array();

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

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

  return $items;
}

A few notes:

  • As mentioned in our previous use of hook_menu(), each item in the array we are returning defines a URL for our Drupal site. The new one we are adding has path 'onthisdate' (e.g. URL http://example.com/onthisdate or http://example.com/?q=onthisdate), given by the array key, and page title 'On this date'.
  • We've told Drupal that if someone visits this URL, the function onthisdate_all() should be used to generate the page content, via the 'page_callback' element of the returned array.
  • We are telling Drupal to allow access to this page to any role with 'access onthisdate content' permission. We defined this permission in Step 3 of the tutorial; you may also need to visit the Administer >> User management >> Permissions page to grant this permission to some roles of users on your site.
  • The type MENU_CALLBACK tells Drupal to not display the link in the site's menu, but instead just use this function when the URL is accessed. Use MENU_NORMAL_ITEM if you want the page link to be in the main Navigation menu.
  • If you want to add more pages to your module, all you need to do is add more items to the $items array in your hook_menu() implementation, and write page display functions to display them.

Try It Out

If the module has not been enabled, enable it as outlined earlier in the tutorial. If you had already enabled the module, in order to reset the menu definitions in the system, you'll need to disable, then re-enable the module, or refresh the menu cache. To clear the cache, go to Administer >> Site configuration >> Performance, scroll to the foot of the page, and click the "Clear cached data" button.

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

See Also

Comments

mustafa678’s picture

hi ,,
when ever I try to get the page I get access denied message.
how could I make this gone, I can't figure out how the access permissions is enforced within the system !! , can any one tell me what's going on please.
thanks in advance.

trevorjfitz’s picture

First off, check and see if you can log on independent of this module. If you can, log on as the admin.

The permissions and access settings are going to be found in the hook_perm() function and in this particular example it looks like this:

function onthisdate_perm(){
	return array('access onthisdate content');
}

(note: the permissions portion of this tutorial can be found here: http://drupal.org/node/206757)

The string 'access onthisdate content' is what you find listed admin >> user management >> permissions area and you need to make sure it is checked for at least whatever roles describe your account.

Chris Einkauf’s picture

In addition to making sure you have given yourself the "access onthisdate content" permission at admin/user/permissions, make sure you clear your cache (at admin/settings/performance).

ookami.kb’s picture

i have the same problem
all permissions are set, tried to disable/enable module, cleared cache but i have this 'access denied' error for all users, even for root

---------------------------------------------------
Sorry, it was my silly mistake - i've written 'page_callback' and not 'page callback'

Pierre Paul Lefebvre’s picture

Why aren't we calling t() for the title/description of the link?

Leksat’s picture

See the "title callback" description in hook_menu(). This comment also helpful.

ltodoran’s picture

I have a problem.
In the second items in the hook if i use 'type'=> MENU_CALLBACK don´t list me the node´s but if I use
'type' => MENU_NORMAL_ITEM, it apears like a new tree menu.
Any solution?

srinath1976’s picture

function that will generate a page with links fonthisdate_all raising

You don't have permission to access /eshopy1/products/ on this server. error
my configuration is
Apache/2.4.27 (Win32) OpenSSL/1.1.0f PHP/7.0.23 Server at localhost Port 81
i am using drupal 6.26 ver.

my item array for this is

$items['products'] = array(
'title' => 'Products',
'page callback' => 'products_all',
'access arguments' => array('access products content'),
'type' => MENU_CALLBACK
);

Please any body help me

Thanks