You are here

function advagg_mod_js_alter in Advanced CSS/JS Aggregation 8.3

Same name and namespace in other branches
  1. 8.4 advagg_mod/advagg_mod.module \advagg_mod_js_alter()
  2. 8.2 advagg_mod/advagg_mod.module \advagg_mod_js_alter()

Implements hook_js_alter().

File

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

Code

function advagg_mod_js_alter(&$js) {
  if (!advagg_enabled()) {
    return;
  }
  $config = \Drupal::config('advagg_mod.settings');

  // Use the current file system path for advagg_mod.
  $module_path = drupal_get_path('module', 'advagg_mod');
  if (isset($js[$module_path . '/js/loadCSS.js'])) {
    if ($config
      ->get('css_defer_js_code') === 4) {
      $js[$module_path . '/js/loadCSS.js']['type'] = 'external';
      $js[$module_path . '/js/loadCSS.js']['data'] = '//cdn.rawgit.com/filamentgroup/loadCSS/master/src/loadCSS.js';
      $js[$module_path . '/js/cssrelpreload.js']['type'] = 'external';
      $js[$module_path . '/js/cssrelpreload.js']['data'] = '//cdn.rawgit.com/filamentgroup/loadCSS/master/src/cssrelpreload.js';
    }
  }

  // Change sort order so aggregates do not get split up.
  if ($config
    ->get('js_adjust_sort_external') || $config
    ->get('js_adjust_sort_browsers')) {
    advagg_mod_sort_css_js($js, 'js');
  }

  // Force all JS to be preprocessed.
  if ($config
    ->get('js_preprocess')) {
    foreach ($js as $path => &$values) {

      // However CKEditor must not be combined or errors *will* occur.
      if ($path == 'core/assets/vendor/ckeditor/ckeditor.js') {
        continue;
      }
      $values['preprocess'] = TRUE;
    }
    unset($values);
  }

  // Move all async JS to the header.
  if ($config
    ->get('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 (!$config
        ->get('js_async') && empty($values['async']) && empty($values['attributes']['async'])) {
        continue;
      }

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