0

I am implementing hook_forms in my custom module and I don't know how to have multiple form submit handlers.

Here is my custom form function:

function data_retrieval_main_form()
{
    $form ['field2'] = array(
        '#type' => 'textfield',
        '#title' => t('Name'),
        '#default_value' => $name,
        '#maxlength' => 127,
    );

    $form ['field3'] = array(
        '#type' => 'textfield',
        '#title' => t('Age'),
        '#default_value' => $age,
        '#maxlength' => 127,
    );

    $form['fileupload'] = array(
        '#title' => t('Choose Image'),
        '#type' => 'file',
    );

    $form ['submit_field'] = array(
        '#type' => 'submit',
        '#default_value' => 'Submit',
    );

    return $form;
}

Here I am implementing hook_forms:

 function hook_forms($form_id, $args) {

      $forms ['mymodule_first_form'] = array(
        'callback' => 'data_retrieval_main_form',
      );


      $forms ['mymodule_second_form'] = array(
        'callback' => 'mymodule_main_form',
        'callback arguments' => array('some parameter'),
      );

      return $forms;
    }

Now I previously had a single form using hook_form and the handler was simply like this:

function data_retrieval_form_submit($form_id, &$form_state)
{
// validation and submission of form 
}

But now that I have multiple forms, how do I have multiple submit handlers for the forms, is there a way in Drupal that checks which form is being submitted?

1

1 Answer 1

2

You can specify a submit handler for each of your forms. Examples:

Override default handler with custom handler (new array):

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

Run custom handler after default (append to array):

$form['#submit'][] = 'data_retrieval_form_submit';

Run custom handler before default (prepend array):

array_unshift($form['#submit'], 'data_retrieval_form_submit');

You can add it to the $form array in a custom form builder function, or alter an existing form via hook_form_alter.

12
  • $form['#submit'] needs to be an array...
    – Clive
    Sep 4, 2015 at 17:00
  • Which function does this go into? Sep 7, 2015 at 8:53
  • I've expanded on my answer. Sep 7, 2015 at 14:03
  • It is not very clear what you mean, I have my form fields in my hook_form function, that is where I specify the submit button: $form ['submit_field'] = array( '#type' => 'submit', '#default_value' => 'Submit', ); so where and how can I specify which form links to which submit handler. You also mention $forms array but the code shows $form array?. Showing which function to put these in will help Sep 7, 2015 at 14:08
  • It is $form (typo). The '#submit' key is not the same as 'submit' (or 'submit_field'), but both do belong to the $form array. Where? Unless you are using a hook (which I have shown), the function is one you declare. In your case one of them appears to be data_retrieval_main_form(). Sep 7, 2015 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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