3 namespace Drupal\advagg\Asset;
5 use Drupal\Core\Asset\AssetCollectionGrouperInterface;
6 use Drupal\Core\Asset\JsCollectionGrouper as CoreJsCollectionGrouper;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
10 * Groups JavaScript assets.
12 class JsCollectionGrouper extends CoreJsCollectionGrouper implements AssetCollectionGrouperInterface {
15 * Module handler service.
17 * @var \Drupal\Core\Extension\ModuleHandlerInterface
19 protected $moduleHandler;
22 * Construct the AssetDumper instance.
24 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
27 public function __construct(ModuleHandlerInterface $module_handler) {
28 $this->moduleHandler = $module_handler;
34 * Puts multiple items into the same group if they are groupable and if they
35 * are for the same browsers. Items of the 'file' type are groupable if their
36 * 'preprocess' flag is TRUE. Items of the 'external' type are not groupable.
38 * Also ensures that the process of grouping items does not change their
39 * relative order. This requirement may result in multiple groups for the same
40 * type and browsers, if needed to accommodate other items in between.
42 public function group(array $js_assets) {
44 // If a group can contain multiple items, we track the information that must
45 // be the same for each item in the group, so that when we iterate the next
46 // item, we can determine if it can be put into the current group, or if a
47 // new group needs to be made for it.
48 $current_group_keys = NULL;
50 foreach ($js_assets as $item) {
51 // The browsers for which the JavaScript item needs to be loaded is part
52 // of the information that determines when a new group is needed, but the
53 // order of keys in the array doesn't matter, and we don't want a new
54 // group if all that's different is that order.
55 ksort($item['browsers']);
57 switch ($item['type']) {
59 // Group file items if their 'preprocess' flag is TRUE.
60 // Help ensure maximum reuse of aggregate files by only grouping
61 // together items that share the same 'group' value.
62 if ($item['preprocess']) {
63 $group_keys = [$item['group'], $item['browsers']];
64 if (isset($item['inline'])) {
65 $group_keys[] = $item['inline'];
74 // Do not group external items.
79 // If the group keys don't match the most recent group we're working with,
80 // then a new group must be made.
81 if ($group_keys !== $current_group_keys) {
83 // Initialize the new group with the same properties as the first item
84 // being placed into it. The item's 'data' and 'weight' properties are
85 // unique to the item and should not be carried over to the group.
86 $groups[$index] = $item;
87 unset($groups[$index]['data'], $groups[$index]['weight']);
88 $groups[$index]['items'] = [];
89 $current_group_keys = $group_keys ? $group_keys : NULL;
92 // Add the item to the current group.
93 $groups[$index]['items'][] = $item;
96 // Run hook so other modules can modify the data.
97 // Call hook_advagg_aggregate_grouping_alter().
99 $this->moduleHandler->alter('advagg_aggregate_grouping', $groups, $type);