tripal_core.ahah.inc 2.4 KB

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