You are here

function advagg_ajax_render_alter in Advanced CSS/JS Aggregation 7.2

Implements hook_ajax_render_alter().

1 string reference to 'advagg_ajax_render_alter'
advagg_admin_settings_form in ./advagg.admin.inc
Form builder; Configure advagg settings.

File

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

Code

function advagg_ajax_render_alter(&$commands) {

  // Do not run hook if AdvAgg is disabled.
  if (!advagg_enabled()) {
    return;
  }

  // Do not run hook if advagg_ajax_render_alter is FALSE.
  if (!variable_get('advagg_ajax_render_alter', ADVAGG_AJAX_RENDER_ALTER)) {
    return;
  }

  // Conditionally adds the default Drupal/jQuery libraries to the page.
  // @see http://drupal.org/node/1279226
  if (function_exists('drupal_add_js_page_defaults')) {
    drupal_add_js_page_defaults();
  }

  // Get Core JS.
  list(, $core_scripts_header, $core_scripts_footer, $items, $settings) = advagg_build_ajax_js_css();

  // Get AdvAgg JS.
  $scripts_header = $scripts_footer = '';
  if (!empty($items['js'])) {
    $scripts_footer_array = advagg_get_js('footer', $items['js'], TRUE);

    // Function advagg_pre_render_scripts() gets called here.
    $scripts_footer = drupal_render($scripts_footer_array);
    $scripts_header_array = advagg_get_js('header', $items['js'], TRUE);

    // Function advagg_pre_render_scripts() gets called here.
    $scripts_header = drupal_render($scripts_header_array);
  }

  // Remove core JS.
  foreach ($commands as $key => $values) {

    // Skip if not an array or not a command.
    if (!is_array($values) || empty($values['command'])) {
      continue;
    }
    if ($values['command'] === 'settings' && is_array($values['settings']) && !empty($values['merge'])) {

      // Remove JS settings.
      unset($commands[$key]);
      continue;
    }
    if ($values['command'] === 'insert' && is_null($values['settings']) && $values['method'] === 'prepend' && $values['data'] == $core_scripts_header) {

      // Remove JS header.
      unset($commands[$key]);
      continue;
    }
    if ($values['command'] === 'insert' && is_null($values['settings']) && $values['method'] === 'append' && $values['data'] == $core_scripts_footer) {

      // Remove JS footer.
      unset($commands[$key]);
      continue;
    }
  }

  // Add in AdvAgg JS.
  $extra_commands = array();
  if (!empty($scripts_header)) {
    $extra_commands[] = ajax_command_prepend('head', $scripts_header);
  }
  if (!empty($scripts_footer)) {
    $extra_commands[] = ajax_command_append('body', $scripts_footer);
  }
  if (!empty($extra_commands)) {
    $commands = array_merge($extra_commands, $commands);
  }
  if (!empty($settings)) {
    array_unshift($commands, ajax_command_settings(advagg_cleanup_settings_array(drupal_array_merge_deep_array(array_filter($settings['data'], 'is_array'))), TRUE));
  }
}