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?

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.

  • $form['#submit'] needs to be an array... – Clive Sep 4 '15 at 17:00
  • Which function does this go into? – user3574492 Sep 7 '15 at 8:53
  • I've expanded on my answer. – othermachines Sep 7 '15 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 – user3574492 Sep 7 '15 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(). – othermachines Sep 7 '15 at 14:19

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.