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:

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'].


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
}

Comments

sugardave’s picture

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

obrigado’s picture

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


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

gnassar’s picture

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?

gnassar’s picture

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

yasirs’s picture

I am saving some problem in calling my submit handler after default handler on Add Outline form. I am trying to add a function to outline_node_form (The form to create outline for Book). I want to execute my function after drupal creates book node type. I created my module and added following code to it but drupal executes my function before inserting book node into databse. All i want it to execute my function after inserting book node to database.
Here is code i am using.

function mymodule_form_alter(&$form, $form_state, $form_id) {   
  if($form_id=="outline_node_form"){
    $form['#submit'][] = 'mymodule_form_mysubmit';
  }
}

function mymodule_form_mysubmit($form, &$form_state){
    /// My code here
}

Drupal executes my function first before inserting book node into database. Can anyone help me on how to make drupal to call my function after inserting book into database? Any help will be really appreciated.

yasirs’s picture

Solved this using hook_nodeapi. Further details can be found at http://api.drupal.org/api/function/hook_nodeapi

landike’s picture

In my case, with Ubercart2 and payment_method settings page _validate() implementation, I used #element_validate against #validate attribute. Not sure why #validate was not worked for me, but still.

Would be great to get info - how Ubercart implememnt mymodule_system_settings_validate().

henryhu’s picture

Though you do not use variable_set() to store them directly. Don't forget delete them with hook_uninstall() in your .install file.

arnoldbird’s picture

Use the form state to alter submitted values...

function my_module_settings_submit($form, &$form_state) {

  if ('yuck bad' === $form_state['values']['my_module_field_name']) {
    $form_state['values']['my_module_field_name'] = t('that\'s more like it');
  }

}