function minifyjs_js_alter in Minify JS 8.2
Same name and namespace in other branches
- 8 minifyjs.module \minifyjs_js_alter()
- 7 minifyjs.module \minifyjs_js_alter()
Implements hook_js_alter().
File
- ./
minifyjs.module, line 65 - Hooks and functions for the minifyjs module.
Code
function minifyjs_js_alter(array &$scripts, AttachedAssetsInterface $assets) {
// Determine if the replacement needs to be performed.
$do_replace = FALSE;
if (\Drupal::config('minifyjs.config')
->get('use_minified')) {
if (\Drupal::config('minifyjs.config')
->get('disable_admin')) {
if (!\Drupal::service('router.admin_context')
->isAdminRoute()) {
$do_replace = TRUE;
}
}
else {
$do_replace = TRUE;
}
}
if ($do_replace) {
// Files array is keyed by file id. To make this operation as fast as
// possible, the array should be keyed by file uri. Also, shorten the
// array by removing files that are not minified.
$files = \Drupal::service('minifyjs')
->loadAllFiles();
$minified_files = [];
foreach ($files as $file) {
if (!empty($file->minified_uri)) {
$minified_files[$file->uri] = $file;
}
}
// Update the scripts array with new file info.
foreach ($scripts as $path => $file) {
if (isset($minified_files[$path])) {
$scripts[$path]['data'] = $minified_files[$path]->minified_uri;
minifyjs_update_scripts($scripts, $path, $minified_files[$path]->minified_uri);
}
}
}
}