18.10.2016 Views

Drupal 7 Module Development

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Building an Admin Interface<br />

Other parameters can be passed into drupal_get_form() in addition to the form<br />

ID. These extra parameters simply get passed through to the callback function for<br />

its own use. We will see how this works later in the chapter.<br />

In <strong>Drupal</strong> 6, drupal_get_form() returned a fully rendered HTML<br />

form. This has been changed in <strong>Drupal</strong> 7 in order to allow more flexibility<br />

in theming and easier form manipulation. drupal_get_form() now<br />

returns an unrendered form array which must be passed to drupal_<br />

render() for final output. In the preceding example the menu system<br />

handles the change transparently, but other code converted from <strong>Drupal</strong> 6<br />

may need to be changed.<br />

Building a form callback function<br />

For the User Warn module we need a form that allows the site administrator to enter<br />

the following items:<br />

• A subject line for our outgoing e-mail<br />

• The text of our outgoing e-mail<br />

• A checkbox indicating whether or not the administrator should be sent a<br />

Bcc on outgoing e-mails<br />

• A submit button<br />

Our menu definition specified user_warn_form as the page arguments, so we need<br />

to create that function and define our form within it.<br />

This function takes two parameters—$form and $form_state. We will<br />

not be using these parameters in the context of just displaying a form. But,<br />

for more information on their usage see the Form API Quickstart Guide.<br />

/**<br />

* Form builder; Create and display the User Warn configuration<br />

* settings form.<br />

*/<br />

function user_warn_form($form, &$form_state) {<br />

// Text field for the e-mail subject.<br />

$form['user_warn_e-mail_subject'] = array(<br />

'#type' => 'textfield',<br />

'#title' => t('Warning e-mail subject'),<br />

'#description' => t('The subject of the e-mail which will be sent<br />

to users.'),<br />

[ 128 ]

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

Saved successfully!

Ooh no, something went wrong!