tripal_core.ahah.inc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /*
  3. * @file
  4. * Part of the Tripal Core API.
  5. *
  6. * Provides support for dynamic forms through AHAH
  7. */
  8. function tripal_core_ahah_init_form() {
  9. // If form elements have autocomplete elements returned in
  10. // an ahah call they won't work because the following JS file
  11. // doesn't get included. If we include it first before any
  12. // ahah calls it will be there when we need it.
  13. drupal_add_js('misc/autocomplete.js');
  14. }
  15. /*
  16. * This function simply gets the posted form ID, builds the form
  17. * and retrieves the specified element
  18. */
  19. function tripal_core_ahah_prepare_form() {
  20. // Retrieve the form from the cache
  21. $form_state = array('storage' => NULL);
  22. $form_build_id = filter_xss($_POST['form_build_id']);
  23. $form = form_get_cache($form_build_id, $form_state);
  24. // Preparing to process the form
  25. $args = $form['#parameters'];
  26. $form_id = array_shift($args);
  27. $form_state['post'] = $form['#post'] = $_POST;
  28. $form['#programmed'] = $form['#redirect'] = FALSE;
  29. // we don't want to submit the form or have required fields validated on
  30. // an ahah callback.
  31. $form_state['submitted'] = TRUE;
  32. $form['#validate'] = NULL;
  33. $form['#submit'] = NULL;
  34. $form_state['submit_handlers'] = NULL;
  35. $form_state['validate_handlers'] = NULL;
  36. tripal_core_ahah_form_element_disable_validation($form);
  37. // Sets the form_state so that the validate and submit handlers can tell
  38. // when the form is submitted via AHAH
  39. $form_state['ahah_submission'] = TRUE;
  40. // Process the form with drupal_process_form. This function calls the submit
  41. // handlers, which put whatever was worthy of keeping into $form_state.
  42. drupal_process_form($form_id, $form, $form_state);
  43. // You call drupal_rebuild_form which destroys $_POST.
  44. // The form generator function is called and creates the form again but since
  45. // it knows to use $form_state, the form will be different.
  46. // The new form gets cached and processed again, but because $_POST is
  47. // destroyed, the submit handlers will not be called again.
  48. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  49. return $form;
  50. }
  51. /*
  52. *
  53. */
  54. function tripal_core_ahah_bind_events() {
  55. // Get the JS settings so we can merge them.
  56. $javascript = drupal_add_js(NULL, NULL, 'header');
  57. $settings = call_user_func_array('array_merge_recursive', $javascript['setting']);
  58. return array('ahah' => $settings['ahah']);
  59. }
  60. /*
  61. *
  62. */
  63. function tripal_core_ahah_form_element_disable_validation(&$form) {
  64. // --START code borrowed from ahah_helper module
  65. foreach (element_children($form) as $child) {
  66. $form[$child]['#validated'] = TRUE;
  67. tripal_core_ahah_form_element_disable_validation($form[$child]);
  68. }
  69. // --END code borrowed from ahah_helper module
  70. }