Chris Muench
Chris Muench

Reputation: 18318

Aggregate and compress CSS files drupal 7

When checking the configuration box for:

Aggregate and compress CSS files, it says saved successfully, but the option does not remain checked and the css files are NOT compressed into one file. Why is this. (Using drupal 7)

Upvotes: 4

Views: 8259

Answers (4)

Solthun
Solthun

Reputation: 157

Most probably you have the option set to a certain value in the settings file.

Upvotes: 0

Aram Boyajyan
Aram Boyajyan

Reputation: 844

This is the solution.

Stylesheets

<?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);

}
?>

Scripts

<?php
function THEME_js_alter(&$js) {

  // Sort JS items, so that they appear in the correct order.
  uasort($js, 'drupal_sort_css_js');

  $weight = 0;

  foreach ($js as $name => $javascript) {
    $js[$name]['group'] = -100;
    $js[$name]['weight'] = ++$weight;
    $js[$name]['every_page'] = 1;
  }

}
?>

Upvotes: 1

user56reinstatemonica8
user56reinstatemonica8

Reputation: 34074

I'm assuming that since you don't mention it, javascript aggregation works?

Drupal's CSS aggregation doesn't (currently) parse the CSS, it just does some regular expressions before combining the files, so it can break if it encounters valid but unusually formatted CSS, or trivially incorrect CSS syntax you'd otherwise never notice.

Check any custom CSS you've made for any CSS errors or quirks (anything unusual), as well as any non-mainstream modules or themes that might have buggy CSS.

To give you an idea, some things that have caused issues include

It might also worth checking that file encodings are consistent. In particular, one known source of weird results in aggregating feeds and files is UTF8 Byte Order Markers (BOM) which can slip into files by mistake. There's a command here which removes all BOM from all files in a directory (use with care!).

If JS aggregation doesn't work either, it's almost certainly a server or directory issue - check files directory permissions, check you have the appropriate compression libraries, plus everything Duncan mentions.

If nothing works, you could try the agrcache module or the Core Library module. They alter the methods used by aggregation and might give a different result, or clues as to the problem. And keep an eye on aggregation issues for Drupal 7 and also Drupal 8 - there might be clues in the issues that people are working on.

Upvotes: 3

Duncan Babbage
Duncan Babbage

Reputation: 20187

If your temporary files directory is mis-configured, then the aggregated CSS (and JS) files cannot be written out, and I believe this option then will switch itself off again. Sometimes this occurs because the path for this directory has been mis-configured. Other times the path is set correctly but there are permissions issues on the directory that mean the webserver can't write to that path. Either way, it prevents the aggregation from working.

The other thing you can do is check the site log for any error messages that may indicate the source of the problem.

Upvotes: 2

Related Questions