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 />

Each argument in a menu item's path can be accessed as an argument that is<br />

available to be passed to our page callback, starting with 0 for the root argument.<br />

So here the string user is item 0, and the user's ID is item 1. To use the user's ID as a<br />

page callback argument, we reference it by its number. The result in this case is that<br />

the user's ID will be passed as an additional argument to drupal_get_form().<br />

We have one other difference in this second menu item:<br />

'type' => MENU_LOCAL_TASK,<br />

We have defined our type as MENU_LOCAL_TASK. This tells <strong>Drupal</strong> that our menu<br />

item describes actions that can be performed on the parent item. In this example,<br />

Warn is an action that can be performed on a user. These are usually rendered as an<br />

additional tab on the page in question, as you can see in the following example user<br />

profile screen:<br />

Having defined the paths for our pages through hook_menu(), we now need to build<br />

our forms.<br />

Form API<br />

In standard web development, one of the most tedious and unrewarding tasks is<br />

defining HTML forms and handling their submissions. Lay out the form, create<br />

labels, write the submission function, figure out error handling, and the worst part<br />

is that from site to site much of this code is boilerplate—it's fundamentally the<br />

same, differing only in presentation. <strong>Drupal</strong>'s Form API is a powerful tool allowing<br />

developers to create forms and handle form submissions quickly and easily. This<br />

is done by defining arrays of form elements and creating validation and submit<br />

callbacks for the form.<br />

[ 126 ]

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

Saved successfully!

Ooh no, something went wrong!