Administration settings forms

Last modified: May 4, 2009 - 07:10

When creating a module you often want to build a form whose sole purpose is to allow some Drupal variables to be set, i.e. the form's submission handler would just be a long list of variable_set's. Drupal provides an easy way to do this.

In your form builder function, use the names of the variables that you want to set for your form element names, and then pass your form array through the system_settings_form() function thus:

<?php
function example_admin_settings() {
 
$form = array();
 
$form['example_var'] = array(
    
// ...
 
);

 
// Pass the form array through system_settings_form()
 
return system_settings_form($form);
}
?>

On submission Drupal will set the variables with the same names as the form elements to the value of those form elements.

Running custom submission handlers

When you want to add additional handlers so the module settings can be automatically saved and perform the additional functions in a separate handler, add to $form['#submit'].

<?php
function example_admin_settings() {
 
$form = array();
 
$form['example_var'] = array(
    
// ...
 
);
 
$form['#submit'][] = 'example_admin_settings_submit';

  return
system_settings_form($form);
}

function
example_admin_settings_submit() {
 
// your additional submission handling code
}
?>

What about validate?

sugardave - March 20, 2009 - 20:50

I can't see to do my own validation on system_settings_form forms. I tried adding to the #validate array, but no dice.

Validation is built in

obrigado - May 22, 2009 - 04:58

In D6, the callback for validation is already there, just write the function. No extra configuration needed.

<?php
function your_system_settings_form_name_validate($form, &$form_state) {
 
// Your validation here!
}
?>

Submit handler additional arguments

gnassar - June 21, 2009 - 09:54

The 5.x version of this article mentions how one can pass additional arguments to the submit handler beyond the defaults. Is there a way to do that in 6.x?

Answered my own question: Not

gnassar - June 21, 2009 - 10:01

Answered my own question:

Not per se. You can't add arguments to submit handlers in 6.x like you could in 5.x; since the submit handler gets the entire form object, you can just declare additional arguments within the form object and have the submit handler look for them.

cf. http://drupal.org/node/144132#custom-params

 
 

Drupal is a registered trademark of Dries Buytaert.