Community Documentation

Administration settings forms

Last updated May 4, 2009. Created by deekayen on February 15, 2008.
Edited by Steven Jones. Log in to edit this page.

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
}
?>

Comments

What about validate?

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

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

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?

George Nassar
President
Provident Data Services

Answered my own question: Not

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

George Nassar
President
Provident Data Services

Custom submission handlers workflow

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.

<?php
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.

Solved

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

#element_validate

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().

@landike (Twitter)

Don't forget to delete in .install

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

About this page

Drupal version
Drupal 6.x
Audience
Programmers
Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here