5

I have a web form with name and email fields. when the form is submitted the name and email should store in database and an PDF file should start download.

my question is how to override the submit function of the web form so that i can add that extra function after that?

Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
user1900662
  • 299
  • 1
  • 7
  • 26
  • look at hook_form_alter() and the submit handlers. examples here: http://drupal.stackexchange.com/questions/18641/how-do-i-alter-the-form-submission-handler – Greg May 15 '13 at 08:04

1 Answers1

14

You will need to create a custom module with hook_form_alter() implementation.

function [YOUR_MODULE]_form_alter(&$form, &$form_state, $form_id)
{
    if($form_id == "YOUR_FORM_ID")
    {
        // target the submit button and add a new submission callback routine to the form
        $form['#submit'][] = 'YOUR_SUBMISSION_CALLBACK_FUNCTION';
    }
}

The code above will execute your new callback function YOUR_SUBMISSION_CALLBACK_FUNCTION AFTER the form's default callback function.

To make your new callback function called BEFORE the form's default callback function, use the following code instead of the giving above:

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

To cancel the form's default callback function and force it to use your function ONLY, use the code below

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

Hope this help.

Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
  • But how do we get the submission values in this submission function? I tried dpm($form) and could not find the values – pal4life Nov 05 '14 at 22:46
  • Coolio found it, had to initialize the function with the parameters as &$form, &$form_state then found the values I wanted – pal4life Nov 05 '14 at 22:48
  • Pay particular attention to the callback, its not within an array. – neelmeg Apr 19 '16 at 20:18