tripal_core_ahah.api.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * @file
  4. * The Tripal AJAX/AHAH API
  5. *
  6. * This file provides the API to help Tripal modules more easily use
  7. * Drupal's AHAH functionality with forms.
  8. */
  9. /**
  10. * @defgroup tripal_ahah_api AHAH API
  11. * @ingroup tripal_core_api
  12. * @{
  13. * Provides an application programming interface (API) for improved
  14. * support of Drupal's AHAH functionality.
  15. *
  16. * @}
  17. */
  18. /**
  19. * This function should be called to initialize the page
  20. * for use of AHAH elements in a form.
  21. *
  22. * See http://tripal.info/documentation/ahah_api for example usage
  23. *
  24. * @returns
  25. * nothing
  26. *
  27. * @ingroup tripal_ahah_api
  28. */
  29. function tripal_core_ahah_init_form() {
  30. // If form elements have autocomplete elements returned in
  31. // an ahah call they won't work because the following JS file
  32. // doesn't get included. If we include it first before any
  33. // ahah calls it will be there when we need it.
  34. drupal_add_js('misc/autocomplete.js');
  35. }
  36. /**
  37. * This function simply gets the posted form ID, builds the form
  38. * and retrieves the specified element. This function should be
  39. * called in an AHAH callback function to retrieve the form
  40. *
  41. * See http://tripal.info/documentation/ahah_api for example usage
  42. *
  43. * @returns
  44. * A Drupal form array
  45. *
  46. * @ingroup tripal_ahah_api
  47. */
  48. function tripal_core_ahah_prepare_form() {
  49. // Retrieve the form from the cache
  50. $form_state = array('storage' => NULL);
  51. $form_build_id = filter_xss($_POST['form_build_id']);
  52. $form = form_get_cache($form_build_id, $form_state);
  53. // Preparing to process the form
  54. $args = $form['#parameters'];
  55. $form_id = array_shift($args);
  56. $form_state['post'] = $form['#post'] = $_POST;
  57. $form['#programmed'] = $form['#redirect'] = FALSE;
  58. // we don't want to submit the form or have required fields validated on
  59. // an ahah callback.
  60. $form_state['submitted'] = TRUE;
  61. $form['#validate'] = NULL;
  62. $form['#submit'] = NULL;
  63. $form_state['submit_handlers'] = NULL;
  64. $form_state['validate_handlers'] = NULL;
  65. tripal_core_ahah_form_element_disable_validation($form);
  66. // Sets the form_state so that the validate and submit handlers can tell
  67. // when the form is submitted via AHAH
  68. $form_state['ahah_submission'] = TRUE;
  69. // Process the form with drupal_process_form. This function calls the submit
  70. // handlers, which put whatever was worthy of keeping into $form_state.
  71. drupal_process_form($form_id, $form, $form_state);
  72. // You call drupal_rebuild_form which destroys $_POST.
  73. // The form generator function is called and creates the form again but since
  74. // it knows to use $form_state, the form will be different.
  75. // The new form gets cached and processed again, but because $_POST is
  76. // destroyed, the submit handlers will not be called again.
  77. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  78. return $form;
  79. }
  80. /**
  81. * This function rebuilds the $settings array needed by the
  82. * javascript that handles AHAH data returns. This function should be
  83. * called in an AHAH callback function prior to returning.
  84. *
  85. * See http://tripal.info/documentation/ahah_api for example usage
  86. *
  87. * @returns
  88. * an associative array with an 'ahah' key.
  89. *
  90. * @ingroup tripal_ahah_api
  91. */
  92. function tripal_core_ahah_bind_events() {
  93. // Get the JS settings so we can merge them.
  94. $javascript = drupal_add_js(NULL, NULL, 'header');
  95. $settings = call_user_func_array('array_merge_recursive', $javascript['setting']);
  96. return array('ahah' => $settings['ahah']);
  97. }
  98. /**
  99. * This function is a helperfunction of the
  100. * tripal_core_ahah_prepare_form() function. It simply
  101. * disables field validations for all fields (recursively) so that
  102. * when the form is rebuilt it doesn't try to validate and submit the form.
  103. *
  104. * See http://tripal.info/documentation/ahah_api for example usage
  105. *
  106. * @returns
  107. * nothing
  108. *
  109. * @ingroup tripal_ahah_api
  110. */
  111. function tripal_core_ahah_form_element_disable_validation(&$form) {
  112. // --START code borrowed from ahah_helper module
  113. foreach (element_children($form) as $child) {
  114. $form[$child]['#validated'] = TRUE;
  115. tripal_core_ahah_form_element_disable_validation($form[$child]);
  116. }
  117. // --END code borrowed from ahah_helper module
  118. }