You are here

function advagg_mod_js_post_alter in Advanced CSS/JS Aggregation 7.2

Alter the js array.

Parameters

array $js: JS array.

File

advagg_mod/advagg_mod.module, line 1918
Advanced aggregation modifier module.

Code

function advagg_mod_js_post_alter(array &$js) {
  if (!module_exists('advagg') || !advagg_enabled()) {
    return;
  }

  // Only add JS if it's actually needed.
  if (variable_get('advagg_mod_js_remove_unused', ADVAGG_MOD_JS_REMOVE_UNUSED)) {
    advagg_mod_remove_js_if_not_used($js);
  }

  // Change sort order so aggregates do not get split up.
  if (variable_get('advagg_mod_js_adjust_sort_external', ADVAGG_MOD_JS_ADJUST_SORT_EXTERNAL) || variable_get('advagg_mod_js_adjust_sort_inline', ADVAGG_MOD_JS_ADJUST_SORT_INLINE) || variable_get('advagg_mod_js_adjust_sort_browsers', ADVAGG_MOD_JS_ADJUST_SORT_BROWSERS)) {
    advagg_mod_sort_css_js($js, 'js');
  }

  // Move JS to the footer.
  advagg_mod_js_move_to_footer($js);

  // Force all JS to be preprocessed.
  if (variable_get('advagg_mod_js_preprocess', ADVAGG_MOD_JS_PREPROCESS)) {
    foreach ($js as &$values) {
      if (!empty($values['preprocess_lock'])) {
        continue;
      }
      $values['preprocess'] = TRUE;
      $values['cache'] = TRUE;
    }
    unset($values);
  }

  // Add the defer or the async tag to JS.
  $jquery_deferred = advagg_mod_js_async_defer($js);

  // Inline JS defer.
  advagg_mod_inline_defer($js, $jquery_deferred);

  // Move all async JS to the header.
  if (variable_get('advagg_mod_js_async_in_header', ADVAGG_MOD_JS_ASYNC_IN_HEADER)) {
    foreach ($js as &$values) {

      // Skip if not file or external.
      if ($values['type'] !== 'file' && $values['type'] !== 'external') {
        continue;
      }

      // Skip if not async.
      if (empty($values['async']) && empty($values['attributes']['async'])) {
        continue;
      }

      // Skip if scope locked.
      if (!empty($values['scope_lock'])) {
        continue;
      }

      // Move to the header with a group of 1000.
      $values['scope'] = 'header';
      $values['group'] = 1000;
    }
    unset($values);
  }
  advagg_mod_prefetch_link($js);
}