function advagg_mod_sort_css_js in Advanced CSS/JS Aggregation 8.4
Same name and namespace in other branches
- 8.2 advagg_mod/advagg_mod.module \advagg_mod_sort_css_js()
- 8.3 advagg_mod/advagg_mod.module \advagg_mod_sort_css_js()
- 7.2 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 browser conditional assets together.
Parameters
array $assets: The CSS or JS array.
string $type: String: css or js.
2 calls to advagg_mod_sort_css_js()
- advagg_mod_css_alter in advagg_mod/
advagg_mod.module - Implements hook_css_alter().
- advagg_mod_js_alter in advagg_mod/
advagg_mod.module - Implements hook_js_alter().
File
- advagg_mod/
advagg_mod.module, line 107 - Advanced aggregation modifier module.
Code
function advagg_mod_sort_css_js(array &$assets, $type) {
$config = \Drupal::config('advagg_mod.settings');
if ($config
->get($type . '_adjust_sort_external')) {
// Find all external items.
$external = [];
$group = NULL;
$weight = NULL;
foreach ($assets as $key => $value) {
// Set values if not set.
if (is_null($group)) {
$group = $value['group'];
}
if (is_null($weight)) {
$weight = $value['weight'];
}
// Find "lightest" item.
if ($value['group'] < $group) {
$group = $value['group'];
}
if ($value['weight'] < $weight) {
$weight = $value['weight'];
}
if (!empty($value['type']) && $value['type'] === 'external' && empty($value['movable'])) {
$external[$key] = $value;
unset($assets[$key]);
}
}
// Sort the array so that it appears in the correct order.
AssetOptimizer::sortStable($external);
// Group all external together.
$offset = 0.0001;
$weight -= 1;
$group -= 10;
$found_jquery = FALSE;
foreach ($external as $key => $value) {
// If bootstrap is used, it must be loaded after jquery. Don't move
// bootstrap if jquery is not above it.
if ($key == 'assets/vendor/jquery/jquery.min.js') {
$found_jquery = TRUE;
}
if (!$found_jquery && (strpos($value['data'], 'bootstrap.min.js') !== FALSE || strpos($value['data'], 'bootstrap.js') !== FALSE)) {
$assets[$key] = $value;
continue;
}
$value['group'] = $group;
$value['weight'] = $weight;
$weight += $offset;
$assets[$key] = $value;
}
}
if ($config
->get($type . '_adjust_sort_browsers')) {
// Get a list of browsers.
$browsers_list = [];
foreach ($assets 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 = [];
$group = NULL;
$weight = NULL;
foreach ($assets 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($weight)) {
$weight = $value['weight'];
}
// Find "heaviest" item.
if ($value['group'] > $group) {
$group = $value['group'];
}
if ($value['weight'] > $weight) {
$weight = $value['weight'];
}
$browsers[$key] = $value;
unset($assets[$key]);
}
}
// Sort the array so that it appears in the correct order.
AssetOptimizer::sortStable($browsers);
// Group all browsers together.
$offset = 0.0001;
$group += 1000;
foreach ($browsers as $key => $value) {
if (isset($value['movable']) && empty($value['movable'])) {
$assets[$key] = $value;
continue;
}
$value['group'] = $group;
$value['weight'] = $weight;
$weight += $offset;
$assets[$key] = $value;
}
}
}
}
}