You are here

ajax_load.module in Ajax Load 6.2

Enable loading of Javascript and CSS data with AJAX loads.

File

ajax_load.module
View source
<?php

/**
 * @file
 * Enable loading of Javascript and CSS data with AJAX loads.
 */

/**
 * Implementation of hook_menu().
 */
function ajax_load_init() {

  // Add on every page because we can't know in advance if an AJAX method
  // will be called.
  // Include the CTools tools that we need.
  ctools_include('ajax');
  ctools_include('modal');

  // Add CTools' javascript to the page.
  ctools_modal_add_js();
  drupal_add_js(drupal_get_path('module', 'ajax_load') . '/ajax_load.js');
}

/**
 * Implementation of hook_ajax_data_alter().
 */
function ajax_load_ajax_data_alter(&$data) {

  //$extra = ajax_data_get_data();
  $array = FALSE;

  // Detect whether the data being altered is an array.
  if (is_array($data)) {
    $data = (object) $data;
    $array = TRUE;
  }

  // use ctools system for preping the shutdown handler on an ajax call
  ini_set('display_errors', 0);
  register_shutdown_function('ctools_shutdown_handler');

  //$data->scripts = $extra['scripts'];

  //$data->css = $extra['css'];
  if (!isset($data->__callbacks)) {
    $data->__callbacks = array();
  }
  if (!isset($data->__commands)) {
    $data->__commands = array();
  }

  // Set the AjaxLoad custom event as a callback.
  $data->__callbacks[] = 'Drupal.CTools.AJAX.commands.ajax_load';

  // use ctools js and css construction
  // also, allow __commands to be used to store ctools ajax commands in ajax_load callbacks
  $data->__commands = ajax_load_ajax_render($data->__commands, FALSE);

  // Cast back to an array if necessary.
  if ($array) {
    $data = (array) $data;
  }
}

/**
 * Render 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.
 */
function ajax_load_ajax_render($commands = array(), $render = TRUE) {
  $js_files = array();
  $settings = ctools_process_js_files($js_files, 'header');
  $settings += ctools_process_js_files($js_files, 'footer');
  $query_string = '?' . substr(variable_get('css_js_query_string', '0'), 0, 1);
  $css = drupal_add_css();
  foreach ($css as $media => $types) {

    // If CSS preprocessing is off, we still need to output the styles.
    // Additionally, go through any remaining styles if CSS preprocessing is on and output the non-cached ones.
    foreach ($types as $type => $files) {
      if ($type == 'module') {

        // Setup theme overrides for module styles.
        $theme_styles = array();
        foreach (array_keys($css[$media]['theme']) as $theme_style) {
          $theme_styles[] = basename($theme_style);
        }
      }

      // The theme stuff should already be added and because of admin themes,
      // this could cause different CSS to be added.
      if ($type != 'theme') {
        foreach ($types[$type] as $file => $preprocess) {

          // If the theme supplies its own style using the name of the module style, skip its inclusion.
          // This includes any RTL styles associated with its main LTR counterpart.
          if ($type == 'module' && in_array(str_replace('-rtl.css', '.css', basename($file)), $theme_styles)) {

            // Unset the file to prevent its inclusion when CSS aggregation is enabled.
            unset($types[$type][$file]);
            continue;
          }

          // Only include the stylesheet if it exists.
          if (file_exists($file)) {
            $css_files[] = array(
              'file' => base_path() . $file . $query_string,
              'media' => $media,
            );
          }
        }
      }
    }
  }
  if (!empty($js_files)) {
    array_unshift($commands, ctools_ajax_command_scripts(array_keys($js_files)));
  }
  if (!empty($css_files)) {
    array_unshift($commands, ctools_ajax_command_css_files($css_files));
  }
  if (!empty($settings)) {
    array_unshift($commands, ctools_ajax_command_settings(call_user_func_array('array_merge_recursive', $settings)));
  }
  if (!empty($_REQUEST['ctools_multipart'])) {

    // We don't use drupal_json here because the header is not true. We're not really
    // returning JSON, strictly-speaking, but rather JSON content wrapped in a <textarea>
    // as per the "file uploads" example here: http://malsup.com/jquery/form/#code-samples
    echo '<textarea>' . drupal_to_js($commands) . '</textarea>';
  }
  else {
    if ($render) {
      drupal_json($commands);
    }
    else {
      return $commands;
    }
  }
  exit;
}

Functions

Namesort descending Description
ajax_load_ajax_data_alter Implementation of hook_ajax_data_alter().
ajax_load_ajax_render Render 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_init Implementation of hook_menu().