Attach a Custom Submit Handler Function to a Drupal 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 tf_form_alter (&$form, $form_state, $form_id) {
  $form['#submit'][] = "tf_form_submit_handler";
}

function tf_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