You are here

function advagg_mod_js_move_to_footer in Advanced CSS/JS Aggregation 8.2

Same name and namespace in other branches
  1. 7.2 advagg_mod/advagg_mod.module \advagg_mod_js_move_to_footer()

Move JS to the footer.

Parameters

array $js: JS array.

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

File

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

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);
}