You are here

function advagg_build_ajax_js_css in Advanced CSS/JS Aggregation 7.2

Gets the core CSS/JS included in this ajax request.

Used so core JS can be rendered through the AdvAgg pipeline.

Return value

array Returns an array containing $styles, $scripts_header, $scripts_footer, $items, and $settings.

See also

ajax_render()

1 call to advagg_build_ajax_js_css()
advagg_ajax_render_alter in ./advagg.module
Implements hook_ajax_render_alter().

File

./advagg.module, line 3794
Advanced CSS/JS aggregation module.

Code

function advagg_build_ajax_js_css() {
  $settings = array();

  // Ajax responses aren't rendered with html.tpl.php, so we have to call
  // drupal_get_css() and drupal_get_js() here, in order to have new files added
  // during this request to be loaded by the page. We only want to send back
  // files that the page hasn't already loaded, so we implement simple diffing
  // logic using array_diff_key().
  foreach (array(
    'css',
    'js',
  ) as $type) {

    // It is highly suspicious if $_POST['ajax_page_state'][$type] is empty,
    // since the base page ought to have at least one JS file and one CSS file
    // loaded. It probably indicates an error, and rather than making the page
    // reload all of the files, instead we return no new files.
    if (empty($_POST['ajax_page_state'][$type])) {
      $core_items[$type] = $items[$type] = array();
      $scripts = drupal_add_js();
      if (!empty($scripts['settings'])) {
        $settings = $scripts['settings'];
      }
    }
    else {
      $function = 'drupal_add_' . $type;

      // Get the current css/js needed for this page.
      $items[$type] = $function();

      // Call hook_js_alter() OR hook_css_alter().
      drupal_alter($type, $items[$type]);

      // Separately track original items that will be used to build the snippets
      // added by core, which will be replaced in advagg_ajax_render_alter().
      $core_items[$type] = $items[$type];
      drupal_alter($type . '_post', $items[$type]);

      // @todo Inline CSS and JS items are indexed numerically. These can't be
      //   reliably diffed with array_diff_key(), since the number can change
      //   due to factors unrelated to the inline content, so for now, we strip
      //   the inline items from Ajax responses, and can add support for them
      //   when drupal_add_css() and drupal_add_js() are changed to use a hash
      //   of the inline content as the array key.
      foreach ($items[$type] as $key => $item) {
        if (is_numeric($key)) {
          unset($items[$type][$key]);
        }
      }
      foreach ($core_items[$type] as $key => $core_item) {
        if (is_numeric($key)) {
          unset($core_items[$type][$key]);
        }
      }

      // Ensure that the page doesn't reload what it already has.
      // @ignore security_17
      $items[$type] = array_diff_key($items[$type], $_POST['ajax_page_state'][$type]);

      // @ignore security_17
      $core_items[$type] = array_diff_key($core_items[$type], $_POST['ajax_page_state'][$type]);
    }
  }

  // Render the HTML to load these files, and add AJAX commands to insert this
  // HTML in the page. We pass TRUE as the $skip_alter argument to prevent the
  // data from being altered again, as we already altered it above. Settings are
  // handled separately, afterwards.
  $scripts = drupal_add_js();
  if (isset($scripts['settings'])) {
    $settings = $scripts['settings'];
    unset($items['js']['settings']);
    unset($core_items['js']['settings']);
  }
  $styles = drupal_get_css($core_items['css'], TRUE);
  $scripts_footer = drupal_get_js('footer', $core_items['js'], TRUE);
  $scripts_header = drupal_get_js('header', $core_items['js'], TRUE);
  return array(
    $styles,
    $scripts_header,
    $scripts_footer,
    $items,
    $settings,
  );
}