ajax_load.module

Tracking 6.x-2.x branch
  1. drupal
    1. 6 contributions/ajax_load/ajax_load.module

Enable loading of Javascript and CSS data with AJAX loads.

Functions & methods

NameDescription
ajax_load_ajax_data_alterImplementation of hook_ajax_data_alter().
ajax_load_ajax_renderRender a commands array into JSON and immediately hand this back to the AJAX requester. We are stealing ctools version of this code. this version is from API 1.8 dev when merlin adds a return argument, this code will be removed.
ajax_load_initImplementation of hook_menu().

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Enable loading of Javascript and CSS data with AJAX loads.
  5. */
  6. /**
  7. * Implementation of hook_menu().
  8. */
  9. function ajax_load_init() {
  10. // Add on every page because we can't know in advance if an AJAX method
  11. // will be called.
  12. // Include the CTools tools that we need.
  13. ctools_include('ajax');
  14. ctools_include('modal');
  15. // Add CTools' javascript to the page.
  16. ctools_modal_add_js();
  17. drupal_add_js(drupal_get_path('module', 'ajax_load') .'/ajax_load.js');
  18. }
  19. /**
  20. * Implementation of hook_ajax_data_alter().
  21. */
  22. function ajax_load_ajax_data_alter(&$data) {
  23. //$extra = ajax_data_get_data();
  24. $array = FALSE;
  25. // Detect whether the data being altered is an array.
  26. if (is_array($data)) {
  27. $data = (object) $data;
  28. $array = TRUE;
  29. }
  30. // use ctools system for preping the shutdown handler on an ajax call
  31. ini_set('display_errors', 0);
  32. register_shutdown_function('ctools_shutdown_handler');
  33. //$data->scripts = $extra['scripts'];
  34. //$data->css = $extra['css'];
  35. if (!isset($data->__callbacks)) {
  36. $data->__callbacks = array();
  37. }
  38. if (!isset($data->__commands)) {
  39. $data->__commands = array();
  40. }
  41. // Set the AjaxLoad custom event as a callback.
  42. $data->__callbacks[] = 'Drupal.CTools.AJAX.commands.ajax_load';
  43. // use ctools js and css construction
  44. // also, allow __commands to be used to store ctools ajax commands in ajax_load callbacks
  45. $data->__commands = ajax_load_ajax_render($data->__commands, FALSE);
  46. // Cast back to an array if necessary.
  47. if ($array) {
  48. $data = (array) $data;
  49. }
  50. }
  51. /**
  52. * Render a commands array into JSON and immediately hand this back
  53. * to the AJAX requester.
  54. * We are stealing ctools version of this code. this version is from API 1.8 dev
  55. * when merlin adds a return argument, this code will be removed.
  56. */
  57. function ajax_load_ajax_render($commands = array(), $render = TRUE) {
  58. $js_files = array();
  59. $settings = ctools_process_js_files($js_files, 'header');
  60. $settings += ctools_process_js_files($js_files, 'footer');
  61. $query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1);
  62. $css = drupal_add_css();
  63. foreach ($css as $media => $types) {
  64. // If CSS preprocessing is off, we still need to output the styles.
  65. // Additionally, go through any remaining styles if CSS preprocessing is on and output the non-cached ones.
  66. foreach ($types as $type => $files) {
  67. if ($type == 'module') {
  68. // Setup theme overrides for module styles.
  69. $theme_styles = array();
  70. foreach (array_keys($css[$media]['theme']) as $theme_style) {
  71. $theme_styles[] = basename($theme_style);
  72. }
  73. }
  74. // The theme stuff should already be added and because of admin themes,
  75. // this could cause different CSS to be added.
  76. if ($type != 'theme') {
  77. foreach ($types[$type] as $file => $preprocess) {
  78. // If the theme supplies its own style using the name of the module style, skip its inclusion.
  79. // This includes any RTL styles associated with its main LTR counterpart.
  80. if ($type == 'module' && in_array(str_replace('-rtl.css', '.css', basename($file)), $theme_styles)) {
  81. // Unset the file to prevent its inclusion when CSS aggregation is enabled.
  82. unset($types[$type][$file]);
  83. continue;
  84. }
  85. // Only include the stylesheet if it exists.
  86. if (file_exists($file)) {
  87. $css_files[] = array(
  88. 'file' => base_path() . $file . $query_string,
  89. 'media' => $media,
  90. );
  91. }
  92. }
  93. }
  94. }
  95. }
  96. if (!empty($js_files)) {
  97. array_unshift($commands, ctools_ajax_command_scripts(array_keys($js_files)));
  98. }
  99. if (!empty($css_files)) {
  100. array_unshift($commands, ctools_ajax_command_css_files($css_files));
  101. }
  102. if (!empty($settings)) {
  103. array_unshift($commands, ctools_ajax_command_settings(call_user_func_array('array_merge_recursive', $settings)));
  104. }
  105. if (!empty($_REQUEST['ctools_multipart'])) {
  106. // We don't use drupal_json here because the header is not true. We're not really
  107. // returning JSON, strictly-speaking, but rather JSON content wrapped in a <textarea>
  108. // as per the "file uploads" example here: http://malsup.com/jquery/form/#code-samples
  109. echo '<textarea>' . drupal_to_js($commands) . '</textarea>';
  110. }
  111. else if ($render) {
  112. drupal_json($commands);
  113. }
  114. else {
  115. return $commands;
  116. }
  117. exit;
  118. }