Letting Drupal know about the new function
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:
<?php
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. URLhttp://example.com/onthisdateorhttp://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
- Menu system
- hook_menu
- Example module and its hook_menu() implementation - another example of how to use hook_menu

access denied
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.
First off, check and see if
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:
<?phpfunction 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.
In addition to making sure
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).