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.

Chapter 5<br />

'#description' => t('Indicates whether the admin user (as set in<br />

site configuration) should be BCC\'d on all warning e-mails.'),<br />

);<br />

}<br />

return system_settings_form($form);<br />

The first thing you'll notice is that we no longer have a submit button element. That<br />

is because system_settings_form() adds one in for us automatically. It gets the<br />

label 'Save settings'.<br />

Additionally, system_settings_form() uses its own custom submit handler<br />

system_settings_form_submit(), which automatically saves all form elements<br />

into persistent variables of the same name. You don't have to write a submit<br />

function at all, <strong>Drupal</strong> takes care of everything behind the scenes.<br />

It might seem silly to use an API function for something as simple as adding a<br />

submit button and automating the handling of persistent variables. However, the<br />

less code you have to write the less bugs you introduce. With just around 30 lines of<br />

code, we now have a fully functional form with extensive validation, customizable<br />

default settings, and the ability for users to change the default settings as they wish.<br />

Having set up our module's configuration form, we now need to add a function that<br />

enables administrators to actually send this e-mail to users.<br />

A shortcut for confirmation forms<br />

Earlier in the chapter, we added a 'Warn' tab to user profile pages. System<br />

administrators should be able to click this tab to send the warning e-mail to users.<br />

However, it would be nice if we could add a confirmation step here, to prevent<br />

e-mails from being sent inadvertently.<br />

This is another situation where <strong>Drupal</strong> offers a convenient shortcut function.<br />

Let's revisit the callback function we looked at earlier while discussing 'value'<br />

form elements.<br />

/**<br />

* Form builder; display the e-mail confirmation form.<br />

*/<br />

function user_warn_confirm_form($form, &$form_state, $uid)<br />

{<br />

$form['account'] = array(<br />

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

'#value' => user_load($uid),<br />

);<br />

[ 139 ]

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

Saved successfully!

Ooh no, something went wrong!