09. Letting Drupal know about the new function
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 the menu() hook. The menu() hook defines the association between a URL and the function that creates the content for that url. The hook also does permission checking, if desired.
<?php
function onthisdate_menu() {
$items = array();
$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_LOCAL_TASK if you want the user to see the link in the side navigation block.
More information on the menu system:
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 be 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.
Download the code so far, (4.7 version) renaming to onthisdate.module before saving in your Drupal installation.
