I have enabled Drupal's Aggregate and compress CSS files. What should I do in order to exclude certain css files from being aggregated and compressed? Thanks.
3 Answers
To stop a file being aggregated, you can add a flag to the drupal_add_css() command which loads the file. To do this, hunt where the CSS is added and add array('preprocess' => FALSE)
after the style, e.g.
drupal_add_css('/fonts/fonts-min.css', array('preprocess' => FALSE));
Alternatively, external CSS are never compressed, so if you change the CSS to the absolute URL of the file it won't be compressed.
drupal_add_css('http://mysite/css/fonts-min.css');
If you want to change a CSS included from a .info file, it might be best to add it as an external file with _preprocess_html
in template.php. This is well explained under 'Adding external stylesheets' on this page.
-
hey, thanks, you are right, but isn't it supposed to be
array('preprocess' => FALSE));
? that's what the doc says.– user2948Jan 31, 2012 at 11:10 -
Oopse, correct, my bad. Copied it out of the defaults so I wouldn't get the spelling wrong :-/– John CJan 31, 2012 at 11:51
I had this problem too, but instead of using drupal_add_css I attached my files to a page like this:
function module_preprocess_page(&$variables) {
$variables['some_element']['#attached'] = array(
'css' => array(
'path/to/css/style.css',
)
)
}
While digging in the attached core code I found that we can use John C's solution here too like this:
function module_preprocess_page(&$variables) {
$variables['some_element']['#attached'] = array(
'css' => array(
'path/to/css/style.css' => array(
'preprocess' => FALSE,
)
)
)
}
How I did this really dirty on Drupal 8 since it's still in dev and too much is changing now:
THEMENAME.theme file:
function THEMENAME_preprocess_html(&$variables) {
$variables['css_address'] = drupal_get_path('theme', 'themename') . '/cssfile.css';
}
And added something like this to head tag in html.html.twig template file:
<link rel="stylesheet" type="text/css" href="{{ css_address }}">
There's going to be a better way soon.