Error message

The spam filter installed on this site is currently unavailable. Per site policy, we are unable to accept new submissions until that problem is resolved. Please try resubmitting the form in a couple of minutes.

Drupal - Attach a Custom Submit Handler Function to a Form

Category: 

Need to add a submit handler function to your Drupal form so you can execute some custom code when a form is submitted? Yes? Well then, shuck 'em up partner, try something like this in your module:

function my_module_form_alter(&$form, $form_state, $form_id) {
  $form['#submit'][] = "my_module_form_submit_handler";
}

function my_module_form_submit_handler($form, &$form_state) {
  drupal_set_message("The form is being submitted, do some extra stuff now...");
}

Typically you will want to limit which forms get your custom submit handler attached to them. Just add some conditionals based on $form_id inside your hook_form_alter.

Add new comment