1

I am displaying a form inside a block and I want it to submit to multiple functions, namely the original submit functions and in addition my own pm_form_submit. Yet I can't get it to work... you can submit to multiple functions right?

In the case bellow only "Test Form Submit" (the original submit function) is displayed.

Submit button of my test form:

function test_form_form ($form,&$form_state){
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );

  return $form;
}

My code for displaying the form in a block:

$form = drupal_get_form ('test_form_form');

//add our submit function (both are here just to test)
$form ['#submit'][] = 'pm_form_submit';
$form ['submit']['#submit'][] = 'pm_form_submit';

$output .= drupal_render ($form);
return $output;

My submit functions: (the pm_form submit is in the same file where I alter the form and the test_form_form_submit is in the file where the form is generated)

function pm_form_submit ($form, &$form_state) {
  drupal_set_message('pm_form_submit');
}

function test_form_form_submit(&$form, &$form_state) {
  drupal_set_message('Test Form Submit');
}

when I print_r the $form array I can see the following:

[submit] => Array
    (
        [#type] => submit
        [#value] => Submit
        [#input] => 1
        [#name] => op
        [#button_type] => submit
        [#executes_submit_callback] => 1
        [#limit_validation_errors] => 
        [#process] => Array
            (
                [0] => ajax_process_form
            )

        [#theme_wrappers] => Array
            (
                [0] => button
            )

        [#defaults_loaded] => 1
        [#tree] => 
        [#parents] => Array
            (
                [0] => submit
            )

        [#weight] => 0.001
        [#processed] => 1
        [#required] => 
        [#attributes] => Array
            (
            )

        [#title_display] => before
        [#id] => edit-submit
        [#ajax_processed] => 
        [#sorted] => 1
        [#submit] => Array
            (
                [0] => pm_form_submit
            )

    )


...
[#submit] => Array
    (
        [0] => test_form_form_submit
        [1] => pm_form_submit
    )
2

You can't add submit handlers to form after drupal_get_form() is called, because they will not be processed.

You need to add submit handler in hook_form_alter(). If you want add this handler only in specific cases, you can add cutom parameter to drupal_get_form()

Code in block:

$form = drupal_get_form('test_form_form', 'add_pm');
$output .= drupal_render($form);
return $output;

Code in custom module:

function MODULE_form_alter(&$form, &$form_state, $form_id) {
   if (($form_id == 'test_form_form') && ($form_state['build_info']['args'][0] == 'add_pm')) {
     // add custom submit only if form was called with parameter. 
     $form ['#submit'][] = 'pm_form_submit';
   }
}

function pm_form_submit($form, &$form_state) {
  drupal_set_message('pm_form_submit');
}
  • or do it in test_form_form() – Andy Aug 9 '12 at 12:48
  • Thx, now I finally understand what this hook_form_alter is for. Yet this posses a different problem.. I can not change the code of the module that contains the form... so I can not add my own custom hook_form_alter there... any ideas on that? – Willem de Vries Aug 9 '12 at 13:45
  • Aa all hook_form_alters are called.;-) THX – Willem de Vries Aug 9 '12 at 13:55
  • @WillemdeVries Typically a form alter is used to alter a form provided by another module. In this case you can also add the submit handler directly in test_form_form(). – Andy Aug 9 '12 at 15:05
  • 1
    @WillemdeVries Sorry, think I must've misunderstood your question. I thought you created the test form yourself - if not, then ignore everything I've said, form alter's the way to go! If you define the form function yourself, then you don't need to use an alter. – Andy Aug 9 '12 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.