Created
April 16, 2012 12:12
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Very Nice!!!
damet garm :D
hi
you must change the hook to your template name
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
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!!!