You are here

function advagg_mod_html_head_alter in Advanced CSS/JS Aggregation 7.2

Implements hook_html_head_alter().

File

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

Code

function advagg_mod_html_head_alter(&$head_elements) {
  if (!module_exists('advagg') || !advagg_enabled()) {
    return;
  }
  foreach ($head_elements as $key => $element) {

    // CSS.
    if (variable_get('advagg_mod_css_head_extract', ADVAGG_MOD_CSS_HEAD_EXTRACT) && !empty($element['#tag']) && $element['#tag'] === 'link' && !empty($element['#attributes']['type']) && $element['#attributes']['type'] === 'text/css' && !empty($element['#attributes']['href'])) {
      $type = 'file';
      if (strpos($element['#attributes']['href'], 'http://') === 0 || strpos($element['#attributes']['href'], 'https://') === 0 || strpos($element['#attributes']['href'], '//') === 0) {
        $type = 'external';
      }
      drupal_add_css($element['#attributes']['href'], array(
        'type' => $type,
        'group' => CSS_SYSTEM,
        'every_page' => TRUE,
        'weight' => -50000,
      ));
      unset($head_elements[$key]);
    }

    // JS.
    if (variable_get('advagg_mod_js_head_extract', ADVAGG_MOD_JS_HEAD_EXTRACT) && !empty($element['#tag']) && $element['#tag'] === 'script' && !empty($element['#attributes']['type']) && $element['#attributes']['type'] === 'text/javascript' && !empty($element['#attributes']['src'])) {
      $type = 'file';
      if (strpos($element['#attributes']['src'], 'http://') === 0 || strpos($element['#attributes']['src'], 'https://') === 0 || strpos($element['#attributes']['src'], '//') === 0) {
        $type = 'external';
      }
      drupal_add_js($element['#attributes']['src'], array(
        'type' => $type,
        'scope' => 'header',
        'group' => JS_LIBRARY,
        'every_page' => TRUE,
        'weight' => -50000,
      ));
      unset($head_elements[$key]);
    }
  }
}