drupal_build_css_cache
- Versions
- 5 – 6
drupal_build_css_cache($types, $filename)- 7
drupal_build_css_cache($css, $filename)
Aggregate and optimize CSS files, putting them in the files directory.
Parameters
$css An array of CSS files to aggregate and compress into one file.
$filename The name of the aggregate CSS file.
Return value
The name of the CSS file.
Code
includes/common.inc, line 3357
<?php
function drupal_build_css_cache($css, $filename) {
$data = '';
// Create the css/ within the files folder.
$csspath = 'public://css';
file_prepare_directory($csspath, FILE_CREATE_DIRECTORY);
if (!file_exists($csspath . '/' . $filename)) {
// Build aggregate CSS file.
foreach ($css as $stylesheet) {
// Only 'file' stylesheets can be aggregated.
if ($stylesheet['type'] == 'file') {
$contents = drupal_load_stylesheet($stylesheet['data'], TRUE);
// Return the path to where this CSS file originated from.
$base = base_path() . dirname($stylesheet['data']) . '/';
_drupal_build_css_path(NULL, $base);
// Prefix all paths within this CSS file, ignoring external and absolute paths.
$data .= preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i', '_drupal_build_css_path', $contents);
}
}
// Per the W3C specification at http://www.w3.org/TR/REC-CSS2/cascade.html#at-import,
// @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;
// Create the CSS file.
file_unmanaged_save_data($data, $csspath . '/' . $filename, FILE_EXISTS_REPLACE);
}
return $csspath . '/' . $filename;
}
?>Login or register to post comments 