Same name in other branches
  1. 5.0.x advagg_mod/advagg_mod.module \advagg_mod_js_alter()
  2. 8.x-2.x advagg_mod/advagg_mod.module \advagg_mod_js_alter()
  3. 8.x-3.x advagg_mod/advagg_mod.module \advagg_mod_js_alter()
  4. 8.x-4.x advagg_mod/advagg_mod.module \advagg_mod_js_alter()

Implements hook_js_alter().

File

advagg_mod/advagg_mod.module, line 13

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::service('extension.list.module')->getPath('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);
    }
}