7

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.

  • 1
    Could you mention what would be it good for? I'm curious. – Scorchio Jan 31 '12 at 12:51
  • I have a css file that changes per node and dont want it to be a part of the large css. – karthik Jan 31 '12 at 13:11
12

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. – karthik Jan 31 '12 at 11:10
  • Oopse, correct, my bad. Copied it out of the defaults so I wouldn't get the spelling wrong :-/ – John C Jan 31 '12 at 11:51
1

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,
      )
    )
  )
}
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.