drupal_add_css
includes/common.inc, строка 1752
- Версии
- 5 – 6
drupal_add_css($path = NULL, $type = 'module', $media = 'all', $preprocess = TRUE)
Добавляет css-файл к списку подгружаемых сss-файлов.
Все CSS файлы, добавляемые модулями, должны иметь префикс с именем данного модуля, например: system-menus.css, а не просто menus.css. В темах оформления эти CSS файлы можно переопределять, используя имена файлов. Префиксы в именах файлов помогут разработчикам тем избежать конфликта имен. Переопределение таблиц стилей происходит в функции drupal_get_css.
If the direction of the current language is right-to-left (Hebrew,
Arabic, etc.), the function will also look for an RTL CSS file and append
it to the list. The name of this file should have an '-rtl.css'
suffix.
For example a CSS file called 'name.css'
will have a 'name-rtl.css'
file added to the list, if exists in the same directory. This CSS file
should contain overrides for properties which should be reversed or
otherwise different in a right-to-left display.
What does this actually mean? CSS preprocessing is the process of aggregating a bunch of separate CSS files into one file that is then compressed by removing all extraneous white space.
The reason for merging the CSS files is outlined quite thoroughly here: http://www.die.net/musings/page_load_time/
'Load fewer external objects. Due to request overhead, one bigger file
just loads faster than two smaller ones half its size.'
However, you should *not* preprocess every file as this can lead to
redundant caches. You should set $preprocess
= FALSE
when:
- Your styles are only used rarely on the site. This could be a special admin page, the homepage, or a handful of pages that does not represent the majority of the pages on your site.
Параметры
$path
(optional) The path to the CSS file relative to the base_path(), e.g.,
/modules/devel/devel.css.
$type
(optional) The type of stylesheet that is being added. Types are: module
or theme.
$media
(optional) The media type for the stylesheet, e.g., all, print, screen.
$preprocess
(optional) Should this CSS file be aggregated and compressed if this
feature has been turned on under the performance section?
Возвращаемое значение
An array of CSS files.
Код
<?php
function drupal_add_css($path = NULL, $type = 'module', $media = 'all', $preprocess = TRUE) {
static $css = array();
global $language;
// Create an array of CSS files for each media type first, since each type needs to be served
// to the browser differently.
if (isset($path)) {
// This check is necessary to ensure proper cascading of styles and is faster than an asort().
if (!isset($css[$media])) {
$css[$media] = array('module' => array(), 'theme' => array());
}
$css[$media][$type][$path] = $preprocess;
// If the current language is RTL, add the CSS file with RTL overrides.
if ($language->direction == LANGUAGE_RTL) {
$rtl_path = str_replace('.css', '-rtl.css', $path);
if (file_exists($rtl_path)) {
$css[$media][$type][$rtl_path] = $preprocess;
}
}
}
return $css;
}
?>
Пример добавления файла CSS в собственном модуле
Реализация хука hook_init:
Файл стилей должен лежать в корне папки модуля.