5 * Hooks provided by the AdvAgg module.
9 * @defgroup advagg_hooks Advanced Aggregates Hooks
12 * Hooks for modules to implement to extend or modify Advanced Aggregates.
14 * @see https://api.drupal.org/api/drupal/includes%21module.inc/group/hooks/7.x
18 * Allow other modules to modify the aggregate groups.
20 * Called once per page at aggregation time (if not cached).
21 * Should be in MODULENAME.advagg.inc file.
23 * @param array $groups
24 * The generated groups.
26 * The asset type ('css' or 'js').
28 * @see \Drupal\advagg\Asset\CssCollectionGrouper::group()
29 * @see \Drupal\advagg\Asset\JsCollectionGrouper::group()
30 * @see advagg_bundler_advagg_aggregate_grouping_alter()
32 function hook_advagg_aggregate_grouping_alter(array &$groups, $type) {
36 foreach ($groups as $key => $group) {
37 if (isset($group['type'], $group['preprocess']) && $group['type'] == 'file' && $group['preprocess']) {
38 $modifiable[$key] = $group;
39 $modifiable[$key]['order'] = $key;
40 $modifiable[$key]['file_count'] = count($group['items']);
41 $files += count($group['items']);
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_files = ceil($files / $max);
55 foreach ($groups as $key => $group) {
56 if (!isset($modifiable[$key]) || $bundles == $max) {
57 $final_groups[] = $group;
60 $splits = round($modifiable[$key]['file_count'] / $target_files);
62 $final_groups[] = $group;
66 $chunks = array_chunk($group['items'], $target_files);
67 foreach ($chunks as $chunk) {
68 $group['items'] = $chunk;
69 $final_groups[] = $group;
74 $groups = $final_groups;
78 * Allow other modules to add in their own settings and hooks.
80 * @param array $aggregate_settings
81 * An associative array of hooks and settings used.
83 * @see advagg_current_hooks_hash_array()
84 * @see advagg_js_minify_advagg_current_hooks_hash_array_alter()
86 function hook_advagg_current_hooks_hash_array_alter(array &$aggregate_settings) {
87 $aggregate_settings['variables']['advagg_js_minify'] = \Drupal::config('advagg_js_minify.settings')->get();
91 * Allow other modules to modify the contents of individual CSS files.
93 * Called once per file at aggregation time.
96 * File contents. Depending on settings/modules these may be minified.
97 * @param array $css_asset
100 * @see \Drupal\advagg\Asset\CssCollectionOptimizer::optimize()
102 function hook_advagg_css_contents_alter(&$data, array $css_asset) {
103 // Remove all font-style rules applying italics.
104 preg_replace("/(.*)(font-style\s*:.*italic)(.*)/m", "$0 --> $1 $3", $data);
108 * Allow other modules to modify the contents of individual JavaScript files.
110 * Called once per file at aggregation time.
112 * @param string $contents
114 * @param array $js_asset
117 * @see \Drupal\advagg\Asset\JsCollectionOptimizer::optimize()
119 function hook_advagg_js_contents_alter(&$contents, array $js_asset) {
120 if ($js_asset['data'] == 'modules/advagg/advagg.admin.js') {
121 $contents = str_replace('AdvAgg Bypass Cookie Removed', 'Advanced Aggregates Cookie Removed', $contents);
126 * Let other modules add/alter additional information about file passed in.
128 * @param string $file
129 * The file path/name.
131 * An associative array of metadata on the file.
132 * @param array $cached
133 * What data was found in the database (if any).
135 * @see Drupal\advagg\State\Files::scanFiles()
137 function hook_advagg_scan_file_alter($file, array $data, array $cached) {
138 $data['chars'] = strlen($data['content']);
142 * Tell advagg about other hooks related to advagg.
144 * @param array $hooks
145 * Array of hooks related to advagg.
147 * If FALSE get only the subset of hooks that alter the filename/contents.
149 * @see advagg_hooks_implemented()
150 * @see advagg_bundler_advagg_hooks_implemented_alter()
152 function hook_advagg_hooks_implemented_alter(array &$hooks, $all) {
154 $hooks['advagg_bundler_analysis_alter'] = [];
159 * Allow other modules to modify the path to save aggregates to.
161 * @param string $path
162 * The currently set folder to save the aggregated assets to.
164 * @see Drupal\advagg\Asset\AssetDumper::preparePath()
165 * @see advagg_mod_advagg_asset_path_alter()
167 function hook_advagg_asset_path_alter(&$path, $extension) {
168 if ($extension == 'js') {
169 $path = 'public://javascript/';
174 * Let other modules modify the analysis array before it is used.
176 * @param array $analysis
177 * An associative array; filename -> data.
179 * @see advagg_bundler_analysis()
181 function hook_advagg_bundler_analysis_alter(array &$analysis) {
182 foreach ($analysis as $filename => &$data) {
184 // This is the filename.
187 // This changes often; 604800 is 1 week.
188 if ($data['changes'] > 10 && $data['mtime'] >= REQUEST_TIME - 604800) {
189 // Modify the group hash so this doesn't end up in a big aggregate.
197 * @} End of "defgroup advagg_hooks".