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

<strong>Drupal</strong> also offers several custom form elements in addition to the standard HTML<br />

fields. You can see an example of one of these in the drupal_get_form() callback<br />

for our second menu item:<br />

/**<br />

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

*/<br />

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

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

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

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

);<br />

}<br />

return confirm_form(<br />

$form,<br />

t('Are you sure you want to send a warning e-mail to this<br />

user?'),<br />

'user/' . $uid,<br />

t('This action can not be undone.'),<br />

t('Send e-mail'),<br />

t('Cancel')<br />

);<br />

We will revisit this function in more detail later in the chapter, but for now we will<br />

focus on the highlighted area. As you'll remember from earlier in this chapter, we<br />

used a wildcard in the menu item path to grab the user's ID and pass it into the page<br />

callback. As you can see now, this is being passed as the third parameter into our<br />

callback function (after the required $form and $form_state parameters). We can<br />

now use this ID to retrieve data about the user for future use.<br />

Also, you can see that we are defining a new form element of type 'value'. The<br />

value element is similar to the HTML hidden fields with two distinct advantages.<br />

First, value elements can contain any data you want as opposed to just strings.<br />

Arrays, objects, or any other complex data structure can be stored and passed in a<br />

value element.<br />

The second advantage is that value elements are not printed back to the browser in<br />

the HTML source. This can improve the security of your data by preventing users<br />

from viewing and/or modifying it on a local instance.<br />

In this code sample we are assigning the value element 'account' with the value of<br />

a <strong>Drupal</strong> user object. This object will be passed on when the form is submitted, and<br />

the receiving function will be able to use it as needed. Value elements are extremely<br />

useful and developers should always consider using them in places where they<br />

would otherwise use hidden fields.<br />

[ 132 ]

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

Saved successfully!

Ooh no, something went wrong!