You are here

function advagg_mod_add_loadcss_js_lib in Advanced CSS/JS Aggregation 7.2

Adds the loadcss js library if needed.

Parameters

array $js: The JS array.

array $css: The CSS array.

2 calls to advagg_mod_add_loadcss_js_lib()
advagg_mod_advagg_modify_css_pre_render_alter in advagg_mod/advagg_mod.module
Implements hook_advagg_modify_css_pre_render_alter().
advagg_mod_page_alter in advagg_mod/advagg_mod.module
Implements hook_page_alter().

File

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

Code

function advagg_mod_add_loadcss_js_lib(array $js = array(), array $css = array()) {
  if (!module_exists('advagg') || !advagg_enabled()) {
    return;
  }

  // Return early if this setting is disabled.
  list(, , , , , , , , , , $css_defer) = advagg_mod_get_lists($js, $css);
  if (empty($css_defer)) {
    return;
  }
  static $added;
  $library = advagg_get_library('loadCSS', 'advagg_mod');
  $options_defaults = advagg_mod_loadcss_js_defaults();
  $preload = '-onload';
  if ($css_defer == 4) {
    $preload = '-preload';
  }
  $css_defer_js_code = variable_get('advagg_mod_css_defer_js_code', ADVAGG_MOD_CSS_DEFER_JS_CODE);

  // Inline load.
  if ($css_defer_js_code == 0) {
    if (!empty($library['installed'])) {
      libraries_load('loadCSS', "inline{$preload}");
    }
    else {
      foreach ($library['variants']["inline{$preload}"]['files']['js'] as $data => $options) {
        if (!isset($added[$data])) {
          if (!empty($options['data'])) {
            drupal_add_js($options['data'], $options + $options_defaults);
            $added[$data] = TRUE;
          }
          else {

            // Fallback to load as a file if no inline js.
            $css_defer_js_code = 2;
          }
        }
      }
    }
  }

  // Load as a file.
  if ($css_defer_js_code == 2) {
    if ($library['installed']) {
      if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 0 && $library['variants']['minified']['#files_exists']) {
        libraries_load('loadCSS', "minified{$preload}");
      }
      else {
        if ($preload) {
          libraries_load('loadCSS');
        }
        else {
          libraries_load('loadCSS', "normal{$preload}");
        }
      }
    }
    else {
      foreach ($library['variants']["local{$preload}"]['files']['js'] as $data => $options) {
        if (!isset($added[$data])) {
          if (!empty($options['data'])) {
            drupal_add_js($options['data'], $options + $options_defaults);
            $added[$data] = TRUE;
          }
          else {

            // Fallback to external load.
            $css_defer_js_code = 4;
          }
        }
      }
    }
  }

  // Load external library.
  if ($css_defer_js_code == 4) {
    foreach ($library['variants']["external{$preload}"]['files']['js'] as $data => $options) {
      if (!isset($added[$data])) {
        drupal_add_js($options['data'], $options + $options_defaults);
        $added[$data] = TRUE;
      }
    }
  }
}