18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5<br />

The <strong>Drupal</strong> menu system<br />

<strong>Drupal</strong>'s menu system is deceptively named. The name implies that it is responsible<br />

for the navigation of your site, and while this is true it does a great deal more. At its<br />

core, the menu system is responsible for mapping <strong>Drupal</strong> paths to the functions that<br />

generate the contents of the requested page. The menu system is also responsible<br />

for controlling access to <strong>Drupal</strong> pages, acting as one of the central gatekeepers of<br />

<strong>Drupal</strong> security.<br />

<strong>Drupal</strong> module developers can map paths to <strong>Drupal</strong> functions by implementing<br />

hook_menu(), which adds paths to the menu system, assigns them access rules,<br />

and optionally creates navigational elements for them.<br />

Defining a page callback with hook_menu<br />

For our module we will need to implement two new pages—a configuration page<br />

for the User Warn module, and a tab in the user profile area where administrators<br />

can go to send the actual e-mails to a specific user. These will each require their own<br />

hook_menu() implementation as defined in the following example.<br />

This example only scratches the surface of the options available in the<br />

menu system. For more details, developers should check out the API<br />

documentation at:<br />

http://api.drupal.org/api/function/hook_menu/7<br />

The example is as follows:<br />

/**<br />

* Implement hook_menu().<br />

*/<br />

function user_warn_menu() {<br />

$items = array();<br />

$items['admin/config/people/user_warn'] = array(<br />

'title' => 'User Warn',<br />

'description' => 'Configuration for the User Warn module.',<br />

'page callback' => 'drupal_get_form',<br />

'page arguments' => array('user_warn_form'),<br />

'access arguments' => array('administer users'),<br />

'type' => MENU_NORMAL_ITEM,<br />

);<br />

[ 121 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!