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.

variable_set('user_warn_e-mail_subject',<br />

$form_state['values']['user_warn_e-mail_subject']);<br />

variable_set('user_warn_e-mail_text',<br />

$form_state['values']['user_warn_e-mail_text']);<br />

variable_set('user_warn_bcc',<br />

$form_state['values']['user_warn_bcc']);<br />

Chapter 5<br />

}<br />

drupal_set_message(t('The settings have been saved'));<br />

After all that, our submit function is pretty simple. We are saving our submitted data<br />

using variable_set(), then setting a simple message indicating that the values<br />

have been saved successfully. Our needs for validation are handled by <strong>Drupal</strong>'s<br />

built-in form validation, so we don't even need a validate function for this data.<br />

The function drupal_set_message() sets a simple message that<br />

is displayed in a specific area at the top of a <strong>Drupal</strong> page. For more<br />

details see http://api.drupal.org/api/function/drupal_<br />

set_message/7.<br />

After a form is submitted, it will reload the form submission page. Since we have<br />

saved new data in our persistent variables through the submit function, and since the<br />

form is pre-loaded with default data based on the data in those variables, we should<br />

now be able to submit new data for these items and see them reflected after the form<br />

has been submitted.<br />

If you want <strong>Drupal</strong> to redirect to a different page after form<br />

submission, you can set $form_state['redirect'] to the desired<br />

path in your submit function. If this isn't working, check whether you<br />

have specified for $form_state to be passed by reference by adding<br />

an ampersand to it in your function signature.<br />

Form API provides us with a great deal of power with a pretty small amount of<br />

work, but <strong>Drupal</strong> offers some shortcuts which make common forms even easier.<br />

[ 137 ]

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

Saved successfully!

Ooh no, something went wrong!