Same name in other branches
  1. 5.0.x advagg_mod/advagg_mod.module \advagg_mod_js_alter()
  2. 6.0.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().

Fichier

advagg_mod/advagg_mod.module, line 12

Code

function advagg_mod_js_alter(&$js) {
    if (\Drupal::moduleHandler()->moduleExists('advagg') && !advagg_enabled()) {
        return;
    }
    $config = \Drupal::config('advagg_mod.settings');
    $css_defer = $config->get('css_defer');
    // Use the current file system path for advagg_mod.
    $module_path = drupal_get_path('module', 'advagg_mod');
    if (advagg_mod_css_defer_active() && isset($js[$module_path . '/js/loadCSS.js'])) {
        if ($css_defer == 3) {
            $js[$module_path . '/js/loadCSS.js']['scope'] = 'header';
            $js[$module_path . '/js/css_defer.js']['scope'] = 'header';
        }
        $css_defer_js_code = $config->get('css_defer_js_code');
        if ($css_defer_js_code == 0) {
            $js[$module_path . '/js/loadCSS.js']['inline'] = TRUE;
            $js[$module_path . '/js/css_defer.js']['inline'] = TRUE;
        }
        elseif ($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';
        }
    }
    // 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');
    }
    // Move JS to the footer.
    if ($config->get('js_footer')) {
        advagg_mod_js_move_to_footer($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;
            $values['cache'] = TRUE;
        }
        unset($values);
    }
    // Add the defer or the async tag to JS.
    advagg_mod_js_async_defer($js);
    // 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 (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);
    }
}