Same name and namespace in other branches
  1. 7.x-2.x advagg_mod/advagg_mod.module \advagg_mod_js_move_to_footer() 1 commentaire

Move JS to the footer.

Paramètres

array $js: JS array.

1 call to advagg_mod_js_move_to_footer()
advagg_mod_js_alter dans advagg_mod/advagg_mod.module
Implements hook_js_alter().

Fichier

advagg_mod/advagg_mod.module, line 230

Code

function advagg_mod_js_move_to_footer(array &$js) {
    // Move all JS to the footer.
    $move_js_to_footer = \Drupal::config('advagg_mod.settings')->get('js_footer');
    $core_header_js = [
        'core/assets/vendor/modernizr/modernizr.min.js' => 0,
        'core/assets/vendor/html5shiv/html5shiv.min.js' => 0,
    ];
    foreach ($js as $key => &$values) {
        // Skip if a core header file and configured to do so.
        if ($move_js_to_footer == 3 && isset($core_header_js[$key])) {
            continue;
        }
        // Skip if the scope has been locked.
        if (!empty($values['scope_lock'])) {
            continue;
        }
        // If JS is not in the header decrease weight by 10000.
        if ($values['scope'] === 'header') {
            $values['weight'] -= 10000;
        }
        // If JS is already in the footer decrease weight by 10000.
        if ($values['scope'] !== 'footer') {
            $values['weight'] -= 10000;
        }
        $values['scope'] = 'footer';
    }
    unset($values);
}