Letting Drupal know about the new function

Last modified: June 10, 2009 - 15:12

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. 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 in tutorial page six. If you had already enabled the module, in order to reset the menu definitions in the system, you'll need to disable, then reenable the module, or refresh the menu cache. To clear the cache, go to Administer >> Settings >> 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

Outdated content

javier.ortiz.llerena - July 4, 2009 - 18:01

Hi,

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

should be:

If the module has not been enabled, enable it as outlined in tutorial page six. If you had already enabled the module, in order to reset the menu definitions in the system, you'll need to disable, then reenable 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.

That is, changing "Administer >> Site configuration >> Performance" for "Administer >> Settings >> Performance"

 
 

Drupal is a registered trademark of Dries Buytaert.