| 5 common.inc | drupal_build_css_cache($types, $filename) |
| 6 common.inc | drupal_build_css_cache( |
| 7 common.inc | drupal_build_css_cache($css) |
| 8 common.inc | drupal_build_css_cache($css) |
Aggregate and optimize CSS files, putting them in the files directory.
Parameters
$types: An array of types of CSS files (e.g., screen, print) to aggregate and compress into one file.
$filename: The name of the aggregate CSS file.
Return value
The name of the CSS file.
Related topics
File
- includes/
common.inc, line 1655 - Common functions that many Drupal modules will need to reference.
Code
<?php
function drupal_build_css_cache($types, $filename) {
$data = '';
// Create the css/ within the files folder.
$csspath = file_create_path('css');
file_check_directory($csspath, FILE_CREATE_DIRECTORY);
if (!file_exists($csspath . '/' . $filename)) {
// Build aggregate CSS file.
foreach ($types as $type) {
foreach ($type as $file => $cache) {
if ($cache) {
$contents = file_get_contents($file);
// Remove multiple charset declarations for standards compliance (and fixing Safari problems)
$contents = preg_replace('/^@charset\s+[\'"](\S*)\b[\'"];/i', '', $contents);
// Return the path to where this CSS file originated from, stripping off the name of the file at the end of the path.
$path = base_path() . substr($file, 0, strrpos($file, '/')) . '/';
// Wraps all @import arguments in url().
$contents = preg_replace('/@import\s+(?!url)[\'"]?(\S*)\b[\'"]?/i', '@import url("\1")', $contents);
// Fix all paths within this CSS file, ignoring absolute paths.
$data .= preg_replace('/url\(([\'"]?)(?![a-z]+:)/i', 'url(\1' . $path . '\2', $contents);
}
}
}
// @import rules must proceed any other style, so we move those to the top.
$regexp = '/@import[^;]+;/i';
preg_match_all($regexp, $data, $matches);
$data = preg_replace($regexp, '', $data);
$data = implode('', $matches[0]) . $data;
// Perform some safe CSS optimizations.
$data = preg_replace('<
\s*([@{}:;,]|\)\s|\s\()\s* | # Remove whitespace around separators, but keep space around parentheses.
/\*([^*\\\\]|\*(?!/))+\*/ | # Remove comments that are not CSS hacks.
[\n\r] # Remove line breaks.
>x', '\1', $data);
// Create the CSS file.
file_save_data($data, $csspath . '/' . $filename, FILE_EXISTS_REPLACE);
}
return $csspath . '/' . $filename;
}
?> Login or register to post comments
Comments
Forcing browser to refresh css files in Drupal 5
Unlike Drupal 6, Drupal 5 does not employ single character query string in css file name,
style.css?AIn case of aggregate css, the query string is included to generate css file name, so it does not need a query string.
<?php$filename = 'css_' . md5(serialize($types) . $query_string) . '.css';
?>
Even though Drupal 5's aggregate css file name looks random using md5 hash function, the "random" file name won't change if you made a few changes to your css files. Then users' browser will not see the change in the css file because the browser sees the same file name and just uses its cached css, and does not bother to fetch new version from the web server.
If you want a quick and dirty way to force user browser refresh of css, we can apply same method used by Drupal 6. That is, add a query string to the css file name.
<?php
// From
$output .= '<style type="text/css" media="'. $media .'">@import "'. base_path() . $preprocess_file .'";</style>'. "\n";
// To (see ?A)
$output .= '<style type="text/css" media="'. $media .'">@import "'. base_path() . $preprocess_file .'?A";</style>'. "\n";
?>
Since the quick hack is meant to be only temporary, it would be okay even if you forgot to undo the change in the code. The change would be lost next time you upgrade common.ini file.
If you want to implement query string method to Drupal 5, we could rewrite function drupal_get_css() using code of Drupal 6. (It would require you to create a new function _drupal_flush_css_js() introduced in Drupal 6). Since we don't want to hack the core we could rename the function (maybe drupal_get_css_hacked) and put it in template.php or a custom module.
Another method that I have used on a site, which is similar to Drupal 6's query string, is to use timestamp of the css file. Instead of using a single letter for the query string, this method gets the modified timestamp (in Unix timestamp) of the css file and uses last 2 or 3 digits for the query string. PHP has a function called stat(), which returns an array of information about a file. One of the information is mtime, which is unix timestamp of last modification.