drupal_get_css
- Versions
- 5 – 7
drupal_get_css($css = NULL)
Returns a themed representation of all stylesheets that should be attached to the page.
It loads the CSS in order, with 'module' first, then 'theme' afterwards. This ensures proper cascading of styles so themes can easily override module styles through CSS selectors.
Themes may replace module-defined CSS files by adding a stylesheet with the same filename. For example, themes/garland/system-menus.css would replace modules/system/system-menus.css. This allows themes to override complete CSS files, rather than specific selectors, when necessary.
If the original CSS file is being overridden by a theme, the theme is responsible for supplying an accompanying RTL CSS file to replace the module's.
Parameters
$css (optional) An array of CSS files. If no array is provided, the default stylesheets array is used instead.
Return value
A string of XHTML CSS tags.
Code
includes/common.inc, line 3241
<?php
function drupal_get_css($css = NULL) {
$output = '';
if (!isset($css)) {
$css = drupal_add_css();
}
$preprocess_css = (variable_get('preprocess_css', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update'));
$directory = file_directory_path('public');
$is_writable = is_dir($directory) && is_writable($directory);
// A dummy query-string is added to filenames, to gain control over
// browser-caching. The string changes on every update or full cache
// flush, forcing browsers to load a new copy of the files, as the
// URL changed.
$query_string = '?' . substr(variable_get('css_js_query_string', '0'), 0, 1);
// Allow modules to alter the css items.
drupal_alter('css', $css);
// Sort css items according to their weights.
uasort($css, 'drupal_sort_weight');
// Remove the overriden CSS files. Later CSS files override former ones.
$previous_item = array();
foreach ($css as $key => $item) {
if ($item['type'] == 'file') {
$basename = basename($item['data']);
if (isset($previous_item[$basename])) {
// Remove the previous item that shared the same base name.
unset($css[$previous_item[$basename]]);
}
$previous_item[$basename] = $key;
}
}
// If CSS preprocessing is off, we still need to output the styles.
// Additionally, go through any remaining styles if CSS preprocessing is on
// and output the non-cached ones.
$css_element = array(
'#tag' => 'link',
'#attributes' => array(
'type' => 'text/css',
'rel' => 'stylesheet',
),
);
$rendered_css = array();
$inline_css = '';
$external_css = '';
$preprocess_items = array();
foreach ($css as $data => $item) {
// Loop through each of the stylesheets, including them appropriately based
// on their type.
switch ($item['type']) {
case 'file':
// Depending on whether aggregation is desired, include the file.
if (!$item['preprocess'] || !($is_writable && $preprocess_css)) {
$element = $css_element;
$element['#attributes']['media'] = $item['media'];
$element['#attributes']['href'] = file_create_url($item['data']) . $query_string;
$rendered_css[] = theme('html_tag', array('element' => $element));
}
else {
$preprocess_items[$item['media']][] = $item;
// Mark the position of the preprocess element,
// it should be at the position of the first preprocessed file.
$rendered_css['preprocess'] = '';
}
break;
case 'inline':
// Include inline stylesheets.
$inline_css .= drupal_load_stylesheet_content($item['data'], $item['preprocess']);
break;
case 'external':
// Preprocessing for external CSS files is ignored.
$element = $css_element;
$element['#attributes']['media'] = $item['media'];
$element['#attributes']['href'] = $item['data'];
$external_css .= theme('html_tag', array('element' => $element));
break;
}
}
if (!empty($preprocess_items)) {
foreach ($preprocess_items as $media => $items) {
// Prefix filename to prevent blocking by firewalls which reject files
// starting with "ad*".
$element = $css_element;
$element['#attributes']['media'] = $media;
$filename = 'css_' . md5(serialize($items) . $query_string) . '.css';
$element['#attributes']['href'] = file_create_url(drupal_build_css_cache($items, $filename));
$rendered_css['preprocess'] .= theme('html_tag', array('element' => $element));
}
}
// Enclose the inline CSS with the style tag if required.
if (!empty($inline_css)) {
$element = $css_element;
$element['#tag'] = 'style';
$element['#value'] = $inline_css;
unset($element['#attributes']['rel']);
$inline_css = "\n" . theme('html_tag', array('element' => $element));
}
// Output all the CSS files with the inline stylesheets showing up last.
return implode($rendered_css) . $external_css . $inline_css;
}
?>Login or register to post comments 