Make drupal form ajax work with GET

Public

JS part

Get raw version
javascript
  1. // Keep old settings.
  2. Drupal.ajax.prototype.originalBeforeSerialize = Drupal.ajax.prototype.beforeSerialize;
  3. Drupal.ajax.prototype.beforeSerialize = function(element, options) {
  4. // Any conditions you need.
  5. if (options.url) {
  6. options.type = 'GET';
  7. return;
  8. }
  9. return this.originalBeforeSerialize(element, options);
  10. };

// It's will be useful to use GET instead of POST if you need to cache requests.
// NOTICE: Will not work with large GET requests due to length limit.

Get raw version
php
  1. // It's will be useful to use GET instead of POST if you need to cache requests.
  2. // NOTICE: Will not work with large GET requests due to length limit.
  3.  
  4. /**
  5.  * Implements hook_menu().
  6.  * Adding new path for ajax callbacks.
  7.  */
  8. function mymodule_menu() {
  9. $items['getform/ajax'] = array(
  10. 'title' => 'AHAH callback',
  11. 'page callback' => 'mymodule_custom_ajax_form_callback',
  12. 'delivery callback' => 'ajax_deliver',
  13. 'access callback' => TRUE,
  14. 'theme callback' => 'ajax_base_page_theme',
  15. 'type' => 0,
  16. );
  17. return $items;
  18. }
  19.  
  20. /**
  21.  * Rewrite standart ajax_form_callback() function.
  22.  */
  23. function mymodule_custom_ajax_form_callback() {
  24. // ajax_get_form -> mymodule_custom_ajax_get_form.
  25. list($form, $form_state, $form_id, $form_build_id, $commands) = mymodule_custom_ajax_get_form();
  26. drupal_process_form($form['#form_id'], $form, $form_state);
  27. if (!empty($form_state['triggering_element'])) {
  28. $callback = $form_state['triggering_element']['#ajax']['callback'];
  29. }
  30. if (!empty($callback) && function_exists($callback)) {
  31. $result = $callback($form, $form_state);
  32. if (!(is_array($result) && isset($result['#type']) && $result['#type'] == 'ajax')) {
  33. $result = array(
  34. '#type' => 'ajax',
  35. '#commands' => ajax_prepare_response($result),
  36. );
  37. }
  38. $result['#commands'] = array_merge($commands, $result['#commands']);
  39. return $result;
  40. }
  41. }
  42.  
  43. /**
  44.  * Rewrite standart ajax_get_form() to work with $_GET.
  45.  */
  46. function mymodule_custom_ajax_get_form() {
  47. $form_state = form_state_defaults();
  48. // GET instead of POST.
  49. $form_build_id = $_GET['form_build_id'];
  50. $form = form_get_cache($form_build_id, $form_state);
  51. if (!$form) {
  52. drupal_exit();
  53. }
  54. $commands = array();
  55. if (isset($form['#build_id_old']) && $form['#build_id_old'] != $form['#build_id']) {
  56. $commands[] = ajax_command_update_build_id($form);
  57. $form_build_id = $form['#build_id'];
  58. }
  59. $form_state['no_redirect'] = TRUE;
  60. $form_state['rebuild_info']['copy']['#build_id'] = TRUE;
  61. $form_state['rebuild_info']['copy']['#action'] = TRUE;
  62. // GET instead of POST.
  63. $form_state['input'] = $_GET;
  64. $form_id = $form['#form_id'];
  65. return array($form, $form_state, $form_id, $form_build_id, $commands);
  66. }
  67.  
  68. // The last thing you need - change ['#ajax']['path'] to 'getform/ajax' on ajax form element.