| 5 common.inc | drupal_get_css($css = NULL) |
| 6 common.inc | drupal_get_css($css = NULL) |
| 7 common.inc | drupal_get_css($css = NULL, $skip_alter = FALSE) |
| 8 common.inc | drupal_get_css($css = NULL, $skip_alter = FALSE) |
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.
See also
File
- includes/
common.inc, line 1879 - Common functions that many Drupal modules will need to reference.
Code
<?php
function drupal_get_css($css = NULL) {
$output = '';
if (!isset($css)) {
$css = drupal_add_css();
}
$no_module_preprocess = '';
$no_theme_preprocess = '';
$preprocess_css = (variable_get('preprocess_css', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update'));
$directory = file_directory_path();
$is_writable = is_dir($directory) && is_writable($directory) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC);
// 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);
foreach ($css as $media => $types) {
// 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.
foreach ($types as $type => $files) {
if ($type == 'module') {
// Setup theme overrides for module styles.
$theme_styles = array();
foreach (array_keys($css[$media]['theme']) as $theme_style) {
$theme_styles[] = basename($theme_style);
}
}
foreach ($types[$type] as $file => $preprocess) {
// If the theme supplies its own style using the name of the module style, skip its inclusion.
// This includes any RTL styles associated with its main LTR counterpart.
if ($type == 'module' && in_array(str_replace('-rtl.css', '.css', basename($file)), $theme_styles)) {
// Unset the file to prevent its inclusion when CSS aggregation is enabled.
unset($types[$type][$file]);
continue;
}
// Only include the stylesheet if it exists.
if (file_exists($file)) {
if (!$preprocess || !($is_writable && $preprocess_css)) {
// If a CSS file is not to be preprocessed and it's a module CSS file, it needs to *always* appear at the *top*,
// regardless of whether preprocessing is on or off.
if (!$preprocess && $type == 'module') {
$no_module_preprocess .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $file . $query_string . '" />' . "\n";
}
// If a CSS file is not to be preprocessed and it's a theme CSS file, it needs to *always* appear at the *bottom*,
// regardless of whether preprocessing is on or off.
else if (!$preprocess && $type == 'theme') {
$no_theme_preprocess .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $file . $query_string . '" />' . "\n";
}
else {
$output .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $file . $query_string . '" />' . "\n";
}
}
}
}
}
if ($is_writable && $preprocess_css) {
// Prefix filename to prevent blocking by firewalls which reject files
// starting with "ad*".
$filename = 'css_' . md5(serialize($types) . $query_string) . '.css';
$preprocess_file = drupal_build_css_cache($types, $filename);
$output .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $preprocess_file . '" />' . "\n";
}
}
return $no_module_preprocess . $output . $no_theme_preprocess;
}
?> Login or register to post comments
Comments
All style tags after the first 30 style tags on an HTML page are
All style tags after the first 30 style tags on an HTML page are not applied. Occasionally, you may also receive the following error message:
The page you are looking for might have been moved or had its name changed.More info: http://support.microsoft.com/kb/262161
Temp fix: Enable "Optimize CSS files" under admin/settings/performance
Module to fix this issue
http://drupal.org/project/unlimited_css
Adding CSS in the template.php via drupal_add_css
For adding css files dynamically in your template.php, this is the zen method. Change 'YOURTEMPLATENAME' to the name of your template.
In template.php:
<?php
// Hypothetical condition about, say, making this only apply to the home page:
/**
* Override or insert variables into the page templates.
*
* @param $vars
* An array of variables to pass to the theme template.
* @param $hook
* The name of the template being rendered ("page" in this case.)
*/
function YOURTEMPLATENAME_preprocess_page(&$vars, $hook) {
// Add a style sheet if something is true, in this case, it's the home page:
if ($vars['is_front']) {
$css = path_to_theme() . '/css/styles2.css';
drupal_add_css($css, 'theme', 'all', 1);
}
// Rebuild Drupal's css array:
$css = drupal_add_css();
// Apply that array to the $styles string to be printed in the <head> section of page.tpl.php
$vars['styles'] = drupal_get_css($css);
}
// */
?>
in page.tpl.php : i use <?php
in page.tpl.php :
i use
<?phpprint $styles;
?>
in include default.css, system.css, etc. but i dont want this.
how to avoid this, please help me!!!!
Remove unwanted styles
You need to unset the array of styles
i.e:
<?php
$css = drupal_add_css();
unset($css['all']['module']['modules/system/system.css']);
unset($css['all']['module']['modules/system/defaults.css']);
unset($css['all']['module']['modules/system/system-menus.css']);
?>
Remove unwanted styles in d6
<?php
/**
* Override or insert PHPTemplate variables into the templates.
*/
function phptemplate_preprocess_page(&$vars) {
// Unset stylesheets.
$css = $vars['css'];
unset($css['all']['module']['modules/node/node.css']);
unset($css['all']['module']['sites/all/modules/contrib/nice_menus/nice_menus.css']);
unset($css['all']['module']['sites/all/modules/contrib/nice_menus/nice_menus_default.css']);
$vars['styles'] = drupal_get_css($css);
}