Return to Snippet

Revision: 23996
at February 18, 2010 09:19 by berkes


Initial Code
/**
 * Implementation of hook_form_alter().
 */
function simple_checkout_form_alter($form_id, &$form) {
  switch($form_id) {
    case 'ideal_payment_api_issuer_form':
      // Add a submit handler.
      // NOTE: we do not override the #submit array, but ADD our own to the array. That way we do not break existing submit-handlers!
      $form['#submit']['simple_checkout_order_submit'] = array();
      // Add a validate handler.
      $form['#validate']['simple_checkout_order_validate'] = array();
      break;
  }
}

// ... A lot of code ...

/** 
 * Fapi callback added in form_alter: validates the orders to our orders table.
 *
 * @param $form_id
 *   Description of param $form_id
 * @param $form_values
 *   Description of param $form_values
 *
 * @return
 *   Nothing.
 */
function simple_checkout_order_validate($form_id, &$form_values) {
  // @TODO: prepare $order and insert into our DB table.
}

/** 
 * Fapi callback added in form_alter: submits the orders to our orders table.
 *
 * @param $form_id
 *   Description of param $form_id
 * @param $form_values
 *   Description of param $form_values
 *
 * @return
 *   Nothing.
 */
function simple_checkout_order_submit($form_id, &$form_values) {
  // @TODO: prepare $order and insert into our DB table.
}

Initial URL
http://wizzlern.nl

Initial Description
By using hook_form_alter, you can add your own callback functions to any Drupal form. 
These functions will be called when Drupal validates the form and when Drupal submits it.

In example below, a module called 'simple_checkout' adds its own handlers to a form called 'ideal_payment_api_issuer_form'. That latter is generated by another module, but we can fill our own tables and run our own validation *in addition to those by the originating module*.

Initial Title
Howto run your own functions on any forms submission.

Initial Tags
forms, drupal, api

Initial Language
PHP