You are here

function ajax_load_ajax_render in Ajax Load 6.2

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.

1 call to ajax_load_ajax_render()
ajax_load_ajax_data_alter in ./ajax_load.module
Implementation of hook_ajax_data_alter().

File

./ajax_load.module, line 63
Enable loading of Javascript and CSS data with AJAX loads.

Code

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;
}