You are here

function advagg_mod_sort_css_js in Advanced CSS/JS Aggregation 7.2

Same name and namespace in other branches
  1. 8.4 advagg_mod/advagg_mod.module \advagg_mod_sort_css_js()
  2. 8.2 advagg_mod/advagg_mod.module \advagg_mod_sort_css_js()
  3. 8.3 advagg_mod/advagg_mod.module \advagg_mod_sort_css_js()

Rearrange CSS/JS so that aggregates are better grouped.

This can move all external assets to the top, thus in one group. This can move all inline assets to the bottom, thus in one group. This can move all browser conditional assets together.

Parameters

array $array: The CSS or JS array.

string $type: String: css or js.

2 calls to advagg_mod_sort_css_js()
advagg_mod_css_post_alter in advagg_mod/advagg_mod.module
Implements hook_css_post_alter().
advagg_mod_js_post_alter in advagg_mod/advagg_mod.module
Alter the js array.

File

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

Code

function advagg_mod_sort_css_js(array &$array, $type) {
  if ($type === 'js' && variable_get('advagg_mod_js_adjust_sort_external', ADVAGG_MOD_JS_ADJUST_SORT_EXTERNAL) || $type === 'css' && variable_get('advagg_mod_css_adjust_sort_external', ADVAGG_MOD_CSS_ADJUST_SORT_EXTERNAL)) {

    // Find all external items.
    $external = array();
    $group = NULL;
    $every_page = NULL;
    $weight = NULL;
    foreach ($array as $key => $value) {

      // Set values if not set.
      if (is_null($group)) {
        $group = $value['group'];
      }
      if (is_null($every_page)) {
        $every_page = $value['every_page'];
      }
      if (is_null($weight)) {
        $weight = $value['weight'];
      }

      // Find "lightest" item.
      if (isset($value['group']) && $value['group'] < $group) {
        $group = $value['group'];
      }
      if (!empty($value['every_page']) && !$every_page) {
        $every_page = $value['every_page'];
      }
      if (isset($value['weight']) && $value['weight'] < $weight) {
        $weight = $value['weight'];
      }
      if (!empty($value['type']) && $value['type'] === 'external') {
        $external[$key] = $value;
        unset($array[$key]);
      }
      if (!empty($value['type']) && $value['type'] === 'inline') {

        // Move jQuery fallback as well.
        if (strpos($value['data'], 'window.jQuery') === 0) {
          $external[$key] = $value;
          unset($array[$key]);
        }

        // Move jQuery ui fallback as well.
        if (strpos($value['data'], 'window.jQuery.ui') === 0) {
          $external[$key] = $value;
          unset($array[$key]);
        }
      }
    }

    // Sort the array so that it appears in the correct order.
    advagg_drupal_sort_css_js_stable($external);

    // Group all external together.
    $offset = 0.0001;
    $weight += -1;
    $found_jquery = FALSE;
    foreach ($external as $key => $value) {
      if (isset($value['movable']) && empty($value['movable'])) {
        $array[$key] = $value;
        continue;
      }

      // If bootstrap is used, it must be loaded after jquery. Don't move
      // bootstrap if jquery is not above it.
      if (strpos($value['data'], 'jquery.min.js') !== FALSE || strpos($value['data'], 'jquery.js') !== FALSE) {
        $found_jquery = TRUE;
      }
      if (!$found_jquery && (strpos($value['data'], 'bootstrap.min.js') !== FALSE || strpos($value['data'], 'bootstrap.js') !== FALSE)) {
        $array[$key] = $value;
        continue;
      }
      $value['group'] = $group;
      $value['every_page'] = $every_page;
      $value['weight'] = $weight;
      $weight += $offset;
      $array[$key] = $value;
    }
  }
  if ($type === 'js' && variable_get('advagg_mod_js_adjust_sort_inline', ADVAGG_MOD_JS_ADJUST_SORT_INLINE) || $type === 'css' && variable_get('advagg_mod_css_adjust_sort_inline', ADVAGG_MOD_CSS_ADJUST_SORT_INLINE)) {

    // Find all inline items.
    $inline = array();
    $group = NULL;
    $every_page = NULL;
    $weight = NULL;
    foreach ($array as $key => $value) {

      // Set values if not set.
      if (is_null($group)) {
        $group = $value['group'];
      }
      if (is_null($every_page)) {
        $every_page = $value['every_page'];
      }
      if (is_null($weight)) {
        $weight = $value['weight'];
      }

      // Find "heaviest" item.
      if (isset($value['group']) && $value['group'] > $group) {
        $group = $value['group'];
      }
      if (empty($value['every_page']) && $every_page) {
        $every_page = FALSE;
      }
      if (isset($value['weight']) && $value['weight'] > $weight) {
        $weight = $value['weight'];
      }
      if (!empty($value['type']) && $value['type'] === 'inline') {

        // Do not move jQuery fallback.
        if (strpos($value['data'], 'window.jQuery') === 0) {
          continue;
        }

        // Do not move jQuery.ui fallback.
        if (strpos($value['data'], 'window.jQuery.ui') === 0) {
          continue;
        }
        $inline[$key] = $value;
        unset($array[$key]);
      }
    }

    // Sort the array so that it appears in the correct order.
    advagg_drupal_sort_css_js_stable($inline);

    // Group all inline together.
    $offset = 0.0001;
    $weight += 1;
    foreach ($inline as $key => $value) {
      if (isset($value['movable']) && empty($value['movable'])) {
        $array[$key] = $value;
        continue;
      }
      $value['group'] = $group;
      $value['every_page'] = $every_page;
      $value['weight'] = $weight;
      $weight += $offset;
      $array[$key] = $value;
    }
  }
  if ($type === 'js' && variable_get('advagg_mod_js_adjust_sort_browsers', ADVAGG_MOD_JS_ADJUST_SORT_BROWSERS) || $type === 'css' && variable_get('advagg_mod_css_adjust_sort_browsers', ADVAGG_MOD_CSS_ADJUST_SORT_BROWSERS)) {

    // Get a list of browsers.
    $browsers_list = array();
    foreach ($array as $key => $value) {
      if (isset($value['browsers']['IE']) && $value['browsers']['IE'] !== TRUE) {
        $browsers_list['IE'][] = $value['browsers']['IE'];
      }
    }

    // Group browsers CSS together.
    if (isset($browsers_list['IE'])) {
      $browsers_list['IE'] = array_values(array_unique($browsers_list['IE']));
      foreach ($browsers_list['IE'] as $browser) {
        $browsers = array();
        $group = NULL;
        $every_page = NULL;
        $weight = NULL;
        foreach ($array as $key => $value) {
          if (isset($value['browsers']['IE']) && $browser === $value['browsers']['IE']) {

            // Set values if not set.
            if (is_null($group)) {
              $group = $value['group'];
            }
            if (is_null($every_page)) {
              $every_page = $value['every_page'];
            }
            if (is_null($weight)) {
              $weight = $value['weight'];
            }

            // Find "heaviest" item.
            if ($value['group'] > $group) {
              $group = $value['group'];
            }
            if (!$value['every_page'] && $every_page) {
              $every_page = $value['every_page'];
            }
            if ($value['weight'] > $weight) {
              $weight = $value['weight'];
            }
            $browsers[$key] = $value;
            unset($array[$key]);
          }
        }

        // Sort the array so that it appears in the correct order.
        advagg_drupal_sort_css_js_stable($browsers);

        // Group all browsers together.
        $offset = 0.0001;
        foreach ($browsers as $key => $value) {
          if (isset($value['movable']) && empty($value['movable'])) {
            $array[$key] = $value;
            continue;
          }
          $value['group'] = $group;
          $value['every_page'] = $every_page;
          $value['weight'] = $weight;
          $weight += $offset;
          $array[$key] = $value;
        }
      }
    }
  }
}