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.

Building an Admin Interface<br />

$items['user/%/warn'] = array(<br />

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

'description' =><br />

'Send e-mail to a user about improper site behavior.',<br />

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

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

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

'type' => MENU_LOCAL_TASK,<br />

);<br />

}<br />

return $items;<br />

Like many <strong>Drupal</strong> hook implementations, hook_menu() returns a structured<br />

associative array with information about the menu items being defined. The first<br />

item in our example defines the module configuration page, and the second one<br />

defines the user tab where administrators can go to send the actual e-mail. Let's<br />

look at the first item in more detail.<br />

Menu items are keyed off their path. This is an internal <strong>Drupal</strong> path with no leading<br />

or trailing slashes. This path not only defines the location of a page, but also its place<br />

in the menu hierarchy, with each part of the URL being a child of the last. In this<br />

example, people is a child of config which is itself a child of admin.<br />

If a requested path does not exist, <strong>Drupal</strong> will work its way up the hierarchy<br />

until it encounters a page that does exist. You can see this in action by requesting<br />

admin/config/people/xyzzy which displays the page at admin/config/people.<br />

If you are creating a menu item for site administration it must begin with admin. This<br />

places it into <strong>Drupal</strong>'s administrative interface and applies the admin theme defined<br />

by the site settings.<br />

<strong>Module</strong>-specific settings should always be present under admin/config.<br />

<strong>Drupal</strong> 7 offers several categories which module developers should use to<br />

better organize their settings according to <strong>Drupal</strong> functional groups like<br />

People and Permissions or Content Authoring.<br />

The value associated with this key is itself an associative array with several keys that<br />

define what action should be taken when this URL is requested. We can now look at<br />

those in detail. The first item defines your page title:<br />

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

[ 122 ]

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

Saved successfully!

Ooh no, something went wrong!