I have noticed that even using “Aggregate and compress CSS files” (admin/config/development/performance) my theme is linking to a bunch of CSS files. Looking deeper, I have seen that every CSS belongs to a group (I couldn't find good documentation on this topic, though… maybe somebody could point me to the right documents to understand better about this topic…). Therefore, there is a CSS file per group.
But actually in the case I'm working on, I don't actually need all this… I can easily go with just one CSS file that combines all these different bits. But keeping the order of loading is important.
So I have created this function inside my theme's template.php:
function THEME_css_alter(&$css) {
// Sort CSS items, so that they appear in the correct order.
// This is taken from drupal_get_css().
uasort($css, 'drupal_sort_css_js');
// The Print style sheets
// I will populate this array with the print css (usually I have only one but just in case…)
$print = array();
// I will add some weight to the new $css array so every element keeps its position
$weight = 0;
foreach ($css as $name => $style) {
// I leave untouched the conditional stylesheets
// and put all the rest inside a 0 group
if ($css[$name]['browsers']['!IE']) {
$css[$name]['group'] = 0;
$css[$name]['weight'] = ++$weight;
$css[$name]['every_page'] = TRUE;
}
// I move all the print style sheets to a new array
if ($css[$name]['media'] == 'print') {
// remove and add to a new array
$print[$name] = $css[$name];
unset($css[$name]);
}
}
// I merge the regular array and the print array
$css = array_merge($css, $print);
}
Using this I obtain only one CSS file plus any print and conditional style sheets.
As this works for me, maybe is useful to others.
Comments
Yes, this is useful. Thanks
Yes, this is useful. Thanks for sharing. It almost seems to me that this might be a D7 bug and your alter function is a work-around. Is anyone that is more knowledgeable about this have something to say about it?
I think similar problems are happening for concatenation of JavaScript.
Combining JavaScript
Yes, you are right: the same is happening with JavaScript files. I will do something similar and post my results here.
And no, so far I haven't received any more comments or thoughts…
I'd look forward to your
I'd look forward to your JavaScript efforts - thanks again for the css_alter.
In order to have all the
In order to have all the JavaScript code combined into one file, I have added the following function (in template.php):
thanks a lot for this. just
thanks a lot for this. just what I was looking for!
Fantastic scripts, thanks for
Fantastic scripts, thanks for sharing! I'm using both the CSS and JS on my Drupal 7 site and managed to reduce the number of files from 8 -> 2!
Thanks a lot for the scripts!
Thanks a lot for the scripts! It has been bothering me for some time and I was looking for a way to do this.
Really Useful
Thx for the snippets, have put it in a custom module, so i don't have to copy it again and again in custom themes. great work - thank you.
Good solutions, appreciate
Good solutions, appreciate the time to post these.
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Great
Thanks for sharing and idea, that realy miss in core.
Maybe, maybe not, its a bit
Maybe, maybe not, its a bit risky because of the 4095 selector limitation in all versions of IE, although that could be worked around by splitting files that hit the limit.
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
thanks for this reply and
thanks for this reply and warned me!
IE CSS Filesize problem
Nice work on the code!
You have to keep in mind that IE supports only 288 KB size of css file. If the file is bigger in size, it will ignore every row in the CSS file after this 288 KB. I'm not absolutely sure about it, but that must be the one of the reasons that Drupal splits the CSS compiled code into few smaller files.
Here's my script for adding
Here's my script for adding all the js to footer:
Could not get your scripts working though.
Thanks it woked
Thanks tomgf,
This code is very helpful in decreasing the HTTP requests on page load
Thank you very much for the code.