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
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.
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?
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
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.
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().
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.
https://muread.com
https://cn.muread.com
How to alter submitted values
Use the form state to alter submitted values...