Skip to content

Instantly share code, notes, and snippets.

@anthonyringoet
Created April 16, 2012 12:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonyringoet/2398328 to your computer and use it in GitHub Desktop.
Save anthonyringoet/2398328 to your computer and use it in GitHub Desktop.
aggregate drupal css and js in ONE file each, not one file per group
<?php
function hook_js_alter(&$js){
uasort($js, 'drupal_sort_css_js');
$i = 0;
foreach($js as $name => $script) {
$js[$name]['weight'] = $i++;
$js[$name]['group'] = JS_DEFAULT;
$js[$name]['every_page'] = FALSE;
$js[$name]['scope'] = 'footer';
}
// exception
$js['sites/all/NEEDS-TO-BE-IN-HEAD.js']['scope'] = 'header';
// header scripts will be in $scripts, so add this in <head>
// footer scripts (most of them) will be in $page_bottom, add before body closing tag
}
function hook_css_alter(&$css){
uasort($css, 'drupal_sort_css_js');
$i = 0;
foreach($css as $name => $script) {
$css[$name]['weight'] = $i++;
$css[$name]['group'] = CSS_DEFAULT;
$css[$name]['every_page'] = FALSE;
}
}
@Gulivert
Copy link

Before all thank you for your script!
I was looking for this script since hours. Im so noob in drupal, but y put this script at first of my template.php, but nothing happen.
Can you help me?
Thank you!!!

@MSHADroo
Copy link

Very Nice!!!
damet garm :D

@MSHADroo
Copy link

@Gulivert

hi
you must change the hook to your template name

@danstn
Copy link

danstn commented May 19, 2014

That's handy.

Also, to keep it cleaner you could modify the iterator by reference (unless you need the $name)
i.e.:
foreach($js as &$script) {
$script['every_page'] = TRUE;
// more stuff
}

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment