Form submit handlers with additional arguments
Asked Answered
R

3

7

For some requirement I need to pass additional information to form submit handler. In form api, while defining custom submit handler as

$additional_args = array();
$form['#submit'][] = 'my_submit_handler'

I expect to submit handler as

function my_submit_handler($form, &$form_state, $additional_args){
Rapt answered 13/8, 2010 at 7:11 Comment(0)
Q
14

The submit handler is called by the drupal fapi, so you can't do something like that. Instead what you can do, is to add what you need, either to the $form, or to the $form_state. The usual approaches is to:

  • Added a field to the form, type value to store the value. Don't do this if you have the value in the form definition.

    $form['store'] = array(
      '#type' => 'value',
      '#value' => $value
    );
    

    This will be available in $form_state['values']['store'].

  • Add the value to $form_state['storage'], done if you variables in your validation handle you want to transfer to your submit handler:

    // Validation.
    $form_state['storage']['value'] = $value;
    
    ...
    
    // Submit
    $value = $form_state['storage']['value'];
    // Need to unset stored values when not used anymore.
    unset($form_state['storage']['value']);
    
Questor answered 13/8, 2010 at 7:45 Comment(1)
Note that as of Drupal 6, you can also simply store arbitrary variables in $form['#foo'] instead, as long as '#foo' does not conflict with any other internal property of the Form API.Jaggers
A
9

Drupal 7: Custom arguments are automatically propagated troug $form_state['build_info']['args'] This is said in http://api.drupal.org/api/drupal/includes!form.inc/function/drupal_get_form/7

Ex:

hook_form($form, &$form_state, $myAdditionnalArg) {...}

Then in

hook_form_submit($form, &$form_state) {

... //$form_state['build_info']['args'] is an array containing at index 0 the value of argument $myAdditionnalArg ...

Anagoge answered 4/10, 2012 at 14:34 Comment(0)
A
4

As reported in $form['#submit'] and $form['#validate'] and $form['#process'] no longer support custom parameters, the suggested way to pass parameters to a submission handler set as in the shown code is to use code similar to the following:

$form['#first_paramater'] = $value;
$form['#submit'][] = 'my_submit_handler';

The handler would retrieve the value as $form['#first_paramater']. To notice that, instead of #first_paramater, the code can use a different string, but it must start with #.

Normally it's not necessary to set a submission handler like the code does, but there are some cases where it is necessary, like to alter a form created by another module, or to set a different submission handler for each of the submission buttons present in a form.

drupal_retrieve_form() saves the parameters passed to the form build handler in $form['#parameters'] which contains:

  • $form_id
  • $form_state
  • parameters passed to the form builder
Antioch answered 14/8, 2010 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.