5 * Advanced aggregation bundler module.
9 * Implements hook_advagg_aggregate_grouping_alter().
11 function advagg_bundler_advagg_aggregate_grouping_alter(&$groups, $type) {
12 // If bundler is disabled then do not do any more processing.
13 if (!advagg_bundler_enabled($type)) {
17 $config = \Drupal::config('advagg_bundler.settings');
18 $max = $config->get('max_' . $type);
19 $logic = $config->get($type . '_logic');
21 $file_info = \Drupal::service('state.advagg.files');
25 foreach ($groups as $key => $group) {
26 if (isset($group['type'], $group['preprocess']) && $group['type'] == 'file' && $group['preprocess']) {
27 $modifiable[$key] = $group;
28 $modifiable[$key]['order'] = $key;
30 $modifiable[$key]['counter'] = count($group['items']);
31 $counter += count($group['items']);
33 elseif ($logic == 1) {
34 $file_info = \Drupal::service('state.advagg.files');
35 $modifiable[$key]['counter'] = 0;
36 foreach ($modifiable[$key]['items'] as &$asset) {
37 $asset['counter'] = $file_info->get($asset['data'])['filesize'];
38 $counter += $asset['counter'];
39 $modifiable[$key]['counter'] += $asset['counter'];
45 // If more bundles than $max return. $groups is already set to least
46 // possible number of groups with current sort. Enabling sort external first
47 // may help decrease number of bundles.
48 if (count($modifiable) > $max || !$modifiable) {
52 $target_count = ceil($counter / $max);
55 foreach ($groups as $key => $group) {
56 if (!isset($modifiable[$key]) || $bundles == $max) {
57 $final_groups[] = $group;
60 $splits = round($modifiable[$key]['counter'] / $target_count);
62 $final_groups[] = $group;
67 $chunks = array_chunk($modifiable[$key]['items'], $target_count);
69 elseif ($logic == 1) {
70 $target_size = $modifiable[$key]['counter'] / $splits;
73 $current_chunk_size = 0;
74 foreach ($modifiable[$key]['items'] as $asset) {
75 $current_chunk_size += $asset['counter'];
76 $chunks[$i][] = $asset;
77 if ($current_chunk_size > $target_size) {
80 $current_chunk_size = 0;
84 foreach ($chunks as $chunk) {
88 $group['items'] = $chunk;
89 $final_groups[] = $group;
94 $groups = $final_groups;