tripal_core_ahah.api.inc

The Tripal AJAX/AHAH API

This file provides the API to help Tripal modules more easily use Drupal's AHAH functionality with forms.

File

tripal_core/api/tripal_core_ahah.api.inc
View source
  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. If no form is available then FALSE is returned
  45. *
  46. * @ingroup tripal_ahah_api
  47. */
  48. function tripal_core_ahah_prepare_form(&$form_state = array()) {
  49. // Retrieve the form from the cache
  50. $form_state['storage'] = NULL;
  51. $form_build_id = filter_xss($_POST['form_build_id']);
  52. if (!$form_build_id) {
  53. return FALSE;
  54. }
  55. $form = form_get_cache($form_build_id, $form_state);
  56. // Preparing to process the form
  57. $args = $form['#parameters'];
  58. if (!is_array($args)) {
  59. // if there is no form #parameters as an array then the form was not built property
  60. return FALSE;
  61. }
  62. $form_id = array_shift($args);
  63. $form_state['post'] = $form['#post'] = $_POST;
  64. $form['#programmed'] = $form['#redirect'] = FALSE;
  65. // we don't want to submit the form or have required fields validated on
  66. // an ahah callback.
  67. $form_state['submitted'] = TRUE;
  68. $form['#validate'] = NULL;
  69. $form['#submit'] = NULL;
  70. $form_state['submit_handlers'] = NULL;
  71. $form_state['validate_handlers'] = NULL;
  72. tripal_core_ahah_form_element_disable_validation($form);
  73. // Sets the form_state so that the validate and submit handlers can tell
  74. // when the form is submitted via AHAH
  75. $form_state['ahah_submission'] = TRUE;
  76. // Process the form with drupal_process_form. This function calls the submit
  77. // handlers, which put whatever was worthy of keeping into $form_state.
  78. drupal_process_form($form_id, $form, $form_state);
  79. // You call drupal_rebuild_form which destroys $_POST.
  80. // The form generator function is called and creates the form again but since
  81. // it knows to use $form_state, the form will be different.
  82. // The new form gets cached and processed again, but because $_POST is
  83. // destroyed, the submit handlers will not be called again.
  84. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  85. return $form;
  86. }
  87. /**
  88. * This function rebuilds the $settings array needed by the
  89. * javascript that handles AHAH data returns. This function should be
  90. * called in an AHAH callback function prior to returning.
  91. *
  92. * See http://tripal.info/documentation/ahah_api for example usage
  93. *
  94. * @returns
  95. * an associative array with an 'ahah' key.
  96. *
  97. * @ingroup tripal_ahah_api
  98. */
  99. function tripal_core_ahah_bind_events() {
  100. // Get the JS settings so we can merge them.
  101. $javascript = drupal_add_js(NULL, NULL, 'header');
  102. $settings = call_user_func_array('array_merge_recursive', $javascript['setting']);
  103. return array('ahah' => $settings['ahah']);
  104. }
  105. /**
  106. * This function is a helperfunction of the
  107. * tripal_core_ahah_prepare_form() function. It simply
  108. * disables field validations for all fields (recursively) so that
  109. * when the form is rebuilt it doesn't try to validate and submit the form.
  110. *
  111. * See http://tripal.info/documentation/ahah_api for example usage
  112. *
  113. * @returns
  114. * nothing
  115. *
  116. * @ingroup tripal_ahah_api
  117. */
  118. function tripal_core_ahah_form_element_disable_validation(&$form) {
  119. // --START code borrowed from ahah_helper module
  120. foreach (element_children($form) as $child) {
  121. $form[$child]['#validated'] = TRUE;
  122. tripal_core_ahah_form_element_disable_validation($form[$child]);
  123. }
  124. // --END code borrowed from ahah_helper module
  125. }