You are here

function advagg_drupal_sort_css_js_stable in Advanced CSS/JS Aggregation 7.2

Same name and namespace in other branches
  1. 8.2 advagg.module \advagg_drupal_sort_css_js_stable()

Stable sort for CSS and JS items.

Preserves the order of items with equal sort criteria.

The function will sort by:

  • $item['group'], integer, ascending
  • $item['every_page'], boolean, first TRUE then FALSE
  • $item['weight'], integer, ascending

Parameters

array &$items: Array of JS or CSS items, as in drupal_add_css() and drupal_add_js(). The array keys can be integers or strings. The items themselves are arrays.

See also

drupal_get_css()

drupal_get_js()

drupal_add_css()

drupal_add_js()

https://drupal.org/node/1388546

3 calls to advagg_drupal_sort_css_js_stable()
advagg_get_css in ./advagg.module
Returns a themed representation of all stylesheets to attach to the page.
advagg_get_js in ./advagg.module
Returns a themed presentation of all JavaScript code for the current page.
advagg_mod_sort_css_js in advagg_mod/advagg_mod.module
Rearrange CSS/JS so that aggregates are better grouped.

File

./advagg.module, line 4688
Advanced CSS/JS aggregation module.

Code

function advagg_drupal_sort_css_js_stable(array &$items) {

  // Within a group, order all infrequently needed, page-specific files after
  // common files needed throughout the website. Separating this way allows for
  // the aggregate file generated for all of the common files to be reused
  // across a site visit without being cut by a page using a less common file.
  $nested = array();
  foreach ($items as $key => &$item) {

    // If weight is not set, make it 0.
    if (!isset($item['weight'])) {
      $item['weight'] = 0;
    }

    // If every_page is not set, make it FALSE.
    if (!isset($item['every_page'])) {
      $item['every_page'] = FALSE;
    }

    // If group is not set, make it CSS_DEFAULT/JS_DEFAULT (0).
    if (!isset($item['group'])) {
      $item['group'] = 0;
    }

    // If scope is not set, make it header.
    if (!isset($item['scope'])) {
      $item['scope'] = 'header';
    }

    // Weight cast to string to preserve float.
    $weight = (string) $item['weight'];
    $nested[$item['group']][$item['every_page'] ? 1 : 0][$weight][$key] = $item;
  }

  // First order by group, so that, for example, all items in the CSS_SYSTEM
  // group appear before items in the CSS_DEFAULT group, which appear before
  // all items in the CSS_THEME group. Modules may create additional groups by
  // defining their own constants.
  $sorted = array();

  // Sort group; then iterate over it.
  ksort($nested);
  foreach ($nested as &$group_items) {

    // Reverse sort every_page; then iterate over it.
    krsort($group_items);
    foreach ($group_items as &$ep_items) {

      // Sort weight; then iterate over it.
      ksort($ep_items);

      // Finally, order by weight.
      foreach ($ep_items as &$weight_items) {
        foreach ($weight_items as $key => &$item) {
          $sorted[$key] = $item;
        }
        unset($item);
      }
    }
    unset($ep_items);
  }
  unset($group_items);
  $items = $sorted;
}