Howto run your own functions on any forms submission.


/ Published in: PHP
Save to your folder(s)

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*.


Copy this code and paste it in your HTML
  1. /**
  2.  * Implementation of hook_form_alter().
  3.  */
  4. function simple_checkout_form_alter($form_id, &$form) {
  5. switch($form_id) {
  6. case 'ideal_payment_api_issuer_form':
  7. // Add a submit handler.
  8. // NOTE: we do not override the #submit array, but ADD our own to the array. That way we do not break existing submit-handlers!
  9. $form['#submit']['simple_checkout_order_submit'] = array();
  10. // Add a validate handler.
  11. $form['#validate']['simple_checkout_order_validate'] = array();
  12. break;
  13. }
  14. }
  15.  
  16. // ... A lot of code ...
  17.  
  18. /**
  19.  * Fapi callback added in form_alter: validates the orders to our orders table.
  20.  *
  21.  * @param $form_id
  22.  * Description of param $form_id
  23.  * @param $form_values
  24.  * Description of param $form_values
  25.  *
  26.  * @return
  27.  * Nothing.
  28.  */
  29. function simple_checkout_order_validate($form_id, &$form_values) {
  30. // @TODO: prepare $order and insert into our DB table.
  31. }
  32.  
  33. /**
  34.  * Fapi callback added in form_alter: submits the orders to our orders table.
  35.  *
  36.  * @param $form_id
  37.  * Description of param $form_id
  38.  * @param $form_values
  39.  * Description of param $form_values
  40.  *
  41.  * @return
  42.  * Nothing.
  43.  */
  44. function simple_checkout_order_submit($form_id, &$form_values) {
  45. // @TODO: prepare $order and insert into our DB table.
  46. }

URL: http://wizzlern.nl

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.