Allow other modules to modify the aggregate groups.

Called once per page at aggregation time (if not cached). Should be in MODULENAME.advagg.inc file.

Parameters

array $groups: The generated groups.

string $type: The asset type ('css' or 'js').

See also

\Drupal\advagg\Asset\CssCollectionGrouper::group()

\Drupal\advagg\Asset\JsCollectionGrouper::group()

advagg_bundler_advagg_aggregate_grouping_alter()

Related topics

1 function implements hook_advagg_aggregate_grouping_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

advagg_bundler_advagg_aggregate_grouping_alter in advagg_bundler/advagg_bundler.advagg.inc
Implements hook_advagg_aggregate_grouping_alter().
2 invocations of hook_advagg_aggregate_grouping_alter()
CssCollectionGrouper::group in src/Asset/CssCollectionGrouper.php
Puts multiple items into the same group if they are groupable and if they are for the same 'browsers' and 'inline'. Items of the 'file' type are groupable if their 'preprocess' flag is TRUE, and items of the 'external' type are never groupable.
JsCollectionGrouper::group in src/Asset/JsCollectionGrouper.php
Puts multiple items into the same group if they are groupable and if they are for the same browsers. Items of the 'file' type are groupable if their 'preprocess' flag is TRUE. Items of the 'external' type are not groupable.

File

./advagg.api.php, line 32

Code

function hook_advagg_aggregate_grouping_alter(array &$groups, $type) {
    $max = 4;
    $modifiable = [];
    $files = 0;
    foreach ($groups as $key => $group) {
        if (isset($group['type'], $group['preprocess']) && $group['type'] == 'file' && $group['preprocess']) {
            $modifiable[$key] = $group;
            $modifiable[$key]['order'] = $key;
            $modifiable[$key]['file_count'] = count($group['items']);
            $files += count($group['items']);
        }
    }
    // If more bundles than $max return. $groups is already set to least
    // possible number of groups with current sort. Enabling sort external first
    // may help decrease number of bundles.
    if (count($modifiable) > $max || !$modifiable) {
        return;
    }
    $target_files = ceil($files / $max);
    $final_groups = [];
    $bundles = 0;
    foreach ($groups as $key => $group) {
        if (!isset($modifiable[$key]) || $bundles == $max) {
            $final_groups[] = $group;
            continue;
        }
        $splits = round($modifiable[$key]['file_count'] / $target_files);
        if ($splits < 2) {
            $final_groups[] = $group;
            $bundles++;
            continue;
        }
        $chunks = array_chunk($group['items'], $target_files);
        foreach ($chunks as $chunk) {
            $group['items'] = $chunk;
            $final_groups[] = $group;
            $bundles++;
        }
    }
    $groups = $final_groups;
}