3 namespace Drupal\advagg\Asset;
5 use Drupal\Core\Asset\AssetCollectionGrouperInterface;
6 use Drupal\Core\Asset\CssCollectionGrouper as CoreCssCollectionGrouper;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8 use Drupal\Core\Extension\ModuleHandlerInterface;
9 use Drupal\Core\State\StateInterface;
14 class CssCollectionGrouper extends CoreCssCollectionGrouper implements AssetCollectionGrouperInterface {
17 * A config object for the advagg configuration.
19 * @var \Drupal\Core\Config\Config
24 * The AdvAgg file status state information storage service.
26 * @var \Drupal\Core\State\StateInterface
29 protected $advaggFiles;
32 * Module handler service.
34 * @var \Drupal\Core\Extension\ModuleHandlerInterface
36 protected $moduleHandler;
39 * Construct the AssetDumper instance.
41 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
42 * A config factory for retrieving required config objects.
43 * @param \Drupal\Core\State\StateInterface $advagg_files
44 * The AdvAgg file status state information storage service.
45 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
48 public function __construct(ConfigFactoryInterface $config_factory, StateInterface $advagg_files, ModuleHandlerInterface $module_handler) {
49 $this->config = $config_factory->get('advagg.settings');
50 $this->advaggFiles = $advagg_files;
51 $this->moduleHandler = $module_handler;
57 * Puts multiple items into the same group if they are groupable and if they
58 * are for the same 'browsers' and 'inline'. Items of the 'file' type are
59 * groupable if their 'preprocess' flag is TRUE, and items of the 'external'
60 * type are never groupable.
62 * Also ensures that the process of grouping items does not change their
63 * relative order. This requirement may result in multiple groups for the same
64 * type, inline, media and browsers, depending on settings if needed to
65 * accommodate other items in between.
67 public function group(array $css_assets) {
68 if ($this->config->get('core_groups')) {
69 return parent::group($css_assets);
71 $combine_media = $this->config->get('css.combine_media');
72 $ie_limit_selectors = $this->config->get('css.ie.limit_selectors');
73 $ie_selector_limit = $this->config->get('css.ie.selector_limit');
74 if ($ie_limit_selectors) {
75 $file_info = $this->advaggFiles->getMultiple(array_column($css_assets, 'data'));
78 // If a group can contain multiple items, we track the information that must
79 // be the same for each item in the group, so that when we iterate the next
80 // item, we can determine if it can be put into the current group, or if a
81 // new group needs to be made for it.
82 $current_group_keys = NULL;
83 // When creating a new group, we pre-increment $i, so by initializing it to
84 // -1, the first group will have index 0.
87 foreach ($css_assets as $item) {
88 // The browsers for which the CSS item needs to be loaded is part of the
89 // information that determines when a new group is needed, but the order
90 // of keys in the array doesn't matter, and we don't want a new group if
91 // all that's different is that order.
92 ksort($item['browsers']);
94 // If the item can be grouped with other items, set $group_keys to an
95 // array of information that must be the same for all items in its group.
96 // If the item can't be grouped with other items, set $group_keys to
97 // FALSE. We put items into a group that can be aggregated together:
98 // whether they will be aggregated is up to the _drupal_css_aggregate()
100 // override of that function specified in hook_css_alter(), but regardless
101 // of the details of that function, a group represents items that can be
102 // aggregated. Since a group may be rendered with a single HTML tag, all
103 // items in the group must share the same information that would need to
104 // be part of that HTML tag.
105 switch ($item['type']) {
107 // Group file items if their 'preprocess' flag is TRUE.
108 // Help ensure maximum reuse of aggregate files by only grouping
109 // together items that share the same 'group' value.
110 if ($item['preprocess']) {
111 $group_keys = [$item['group'], $item['browsers']];
112 if (!$combine_media) {
113 $group_keys[] = $item['media'];
115 if (isset($item['inline'])) {
116 $group_keys[] = $item['inline'];
118 if ($ie_limit_selectors) {
119 if (isset($file_info[$item['data']]['parts'])) {
120 foreach ($file_info[$item['data']]['parts'] as $part) {
123 unset($groups[$i]['data'], $groups[$i]['weight'], $groups[$i]['basename']);
124 $groups[$i]['items'] = [0 => $item];
125 $groups[$i]['items'][0]['data'] = $part['path'];
126 $selectors = $part['selectors'];
130 $selectors += $file_info[$item['data']]['linecount'];
131 if ($selectors > $ie_selector_limit) {
132 $group_keys['break'] = TRUE;
139 if ($ie_limit_selectors && $file_info[$item['data']]['linecount'] > $ie_selector_limit) {
140 foreach ($file_info[$item['data']]['parts'] as $part) {
143 unset($groups[$i]['data'], $groups[$i]['weight'], $groups[$i]['basename']);
144 $groups[$i]['items'] = [0 => $item];
145 $groups[$i]['items'][0]['data'] = $part['path'];
153 // Do not group external items.
157 // If the group keys don't match the most recent group we're working with,
158 // then a new group must be made.
159 if ($group_keys !== $current_group_keys) {
160 if ($ie_limit_selectors) {
161 if ($item['type'] == 'file' && $item['preprocess']) {
162 $selectors = $file_info[$item['data']]['linecount'];
168 unset($group_keys['break']);
170 // Initialize the new group with the same properties as the first item
171 // being placed into it. The item's 'data', 'weight' and 'basename'
172 // properties are unique to the item and should not be carried over to
175 unset($groups[$i]['data'], $groups[$i]['weight'], $groups[$i]['basename']);
176 if ($combine_media && $item['type'] == 'file' && $item['preprocess']) {
177 unset($groups[$i]['media']);
179 $groups[$i]['items'] = [];
180 $current_group_keys = $group_keys ? $group_keys : NULL;
183 // Add the item to the current group.
184 $groups[$i]['items'][] = $item;
187 // Run hook so other modules can modify the data.
188 // Call hook_advagg_asset_grouping_alter().
190 $this->moduleHandler->alter('advagg_aggregate_grouping', $groups, $type);