Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
function NAMEOFTHEME_preprocess_page(&$vars) {
// Use grouped import technique for more than 30 un-aggregated stylesheets (css limit fix for IE)
$css = drupal_add_css();
if (NAMEOFTHEME_css_count($css) > 26) {
$styles = '';
$suffix = "\n".'</style>'."\n";
foreach ($css as $media => $types) {
$prefix = '<style type="text/css" media="'. $media .'">'."\n";
$imports = array();
foreach ($types as $files) {
foreach ($files as $file => $preprocess) {
$imports[] = '@import "'. base_path() . $file .'";';
if (count($imports) == 30) {
$styles .= $prefix . implode("\n", $imports) . $suffix;
$imports = array();
}
}
}
$styles .= (count($imports) > 0) ? ($prefix . implode("\n", $imports) . $suffix) : '';
}
$vars['styles'] = $styles;
}
else {
$vars['styles'] = drupal_get_css(); // Use normal link technique
}
}
/**
* Count the total number of CSS files in $vars['css']
*/
function NAMEOFTHEME_css_count($array) {
$count = 0;
foreach ($array as $item) {
$count = (is_array($item)) ? $count + NAMEOFTHEME_css_count($item) : $count + 1;
}
return $count;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment