drupal_add_css

Versions
mediamosa-21
drupal_add_css($data = NULL, $options = NULL)

Adds a cascading stylesheet to the stylesheet queue.

Calling drupal_static_reset('drupal_add_css') will clear all cascading stylesheets added so far.

If preprocessing is turned on, the cascading style sheets added using this function will be preprocessed before they are added to the HTML header of the page. Preprocessing merges all the CSS files into one file, which is then compressed by removing all extraneous white space. Note that preprocessed inline stylesheets will not be aggregated into this single file; instead, they will just be compressed when being output on the page. External stylesheets will also not be aggregated.

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 $options['preprocess'] to FALSE when your styles are only used on a few pages of 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.

Typical candidates for preprocessing are for example styles for nodes across the site, or styles used in the theme.

Parameters

$data (optional) The stylesheet data to be added, depending on what is passed through to the $options['type'] parameter:

  • 'file': The path to the CSS file relative to the base_path(), e.g., "modules/devel/devel.css". Note that Modules should always prefix the names of their CSS files with the module name; for example, system-menus.css rather than simply menus.css. Themes can override module-supplied CSS files based on their filenames, and this prefixing helps prevent confusing name collisions for theme developers. See drupal_get_css() where the overrides are performed. Also, 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 'mymodule-name.css' will have a 'mymodule-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.
  • 'inline': A string of CSS that should be placed in the given scope. Note that it is better practice to use 'file' stylesheets, rather than 'inline', as the CSS would then be aggregated and cached.
  • 'external': The absolute path to an external CSS file that is not hosted on the local server. These files will not be aggregated if CSS aggregation is enabled.

$options (optional) A string defining the 'type' of CSS that is being added in the $data parameter ('file', 'inline', or 'external'), or an array which can have any or all of the following keys:

  • 'type': The type of stylesheet being added. Available options are 'file', 'inline' or 'external'. Defaults to 'file'.
  • 'weight': The weight of the stylesheet specifies the order in which the CSS will appear when presented on the page. Available constants are:

    • CSS_SYSTEM: Any system-layer CSS.
    • CSS_DEFAULT: Any module-layer CSS.
    • CSS_THEME: Any theme-layer CSS.

    If you need to embed a CSS file before any other module's stylesheets, for example, you would use CSS_DEFAULT - 1. Note that inline CSS is simply appended to the end of the specified scope (region), so they always come last.

  • 'media': The media type for the stylesheet, e.g., all, print, screen. Defaults to 'all'.
  • 'preprocess': If TRUE, Allows the CSS to be aggregated and compressed if the Optimize CSS feature has been turned on under the performance section. Defaults to TRUE.
  • 'browsers': An array containing information specifying which browsers should load the CSS item. See drupal_pre_render_conditional_comments() for details.

Return value

An array of queued cascading stylesheets.

▾ 50 functions call drupal_add_css()

aggregator_init in modules/aggregator/aggregator.module
Implements hook_init().
block_admin_demo in modules/block/block.admin.inc
Menu callback for admin/structure/block/demo.
block_admin_display_form in modules/block/block.admin.inc
Generate main blocks administration form.
book_init in modules/book/book.module
Implements hook_init().
coder_review_form_alter in sites/all/modules/coder/coder_review/coder_review.module
Implements hook_form_alter().
coder_review_page_form in sites/all/modules/coder/coder_review/coder_review.module
Implements hook_form().
common_test_js_and_css_querystring in modules/simpletest/tests/common_test.module
Adds a JavaScript file and a CSS file with a query string appended.
dblog_init in modules/dblog/dblog.module
Implements hook_init().
devel_init in sites/all/modules/devel/devel.module
Implements hook_init().
devel_print_object in sites/all/modules/devel/devel.module
Display an object or array
devel_themer_init in sites/all/modules/devel/devel_themer.module
drupal_get_css in includes/common.inc
Returns a themed representation of all stylesheets that should be attached to the page.
forum_init in modules/forum/forum.module
Implements hook_init().
garland_preprocess_html in themes/garland/template.php
Override or insert variables into the html template.
help_main in modules/help/help.admin.inc
Menu callback; prints a page listing a glossary of Drupal terminology.
hook_init in modules/system/system.api.php
Perform setup tasks. See also, hook_boot.
locale_language_switcher_session in includes/locale.inc
Return the session language switcher block.
locale_translate_seek_screen in modules/locale/locale.admin.inc
String search screen.
mediamosa_browse_asset_list_form in sites/all/modules/mediamosa/maintenance/browse/mediamosa_maintenance_browse_asset.inc
Form builder; asset listing page.
mediamosa_browse_collection_list_form in sites/all/modules/mediamosa/maintenance/browse/mediamosa_maintenance_browse_collection.inc
Form builder; collection listing page.
mediamosa_development_rest_form in sites/all/modules/mediamosa_development/mediamosa_development.admin.inc
The form to execute a REST call.
mediamosa_maintenance_browse_revision_form in sites/all/modules/mediamosa/maintenance/browse/mediamosa_maintenance_browse_revision.inc
Show listing of the revisions (of our nodes).
mediamosa_statistics_filter_form in sites/all/modules/mediamosa/modules/statistics/mediamosa_statistics.inc
Form builder; Return form for mediafile filters.
node_init in modules/node/node.module
Implements hook_init().
openid_user_identities in modules/openid/openid.pages.inc
Menu callback; Manage OpenID identities for the specified user.
overlay_init in modules/overlay/overlay.module
Implements hook_init().
poll_init in modules/poll/poll.module
Implements hook_init().
search_form in modules/search/search.module
Render a search form.
seven_preprocess_html in themes/seven/template.php
Override or insert variables into the html template.
simpletest_result_form in modules/simpletest/simpletest.pages.inc
Test results form for $test_id.
system_init in modules/system/system.module
Implements hook_init().
template_preprocess_field_ui_field_overview_form in modules/field_ui/field_ui.admin.inc
Theme preprocess function for field_ui-field-overview-form.tpl.php.
template_process_html in includes/theme.inc
Process variables for html.tpl.php
template_process_maintenance_page in includes/theme.inc
The variables array generated here is a mirror of template_process_html(). This processor will run its course when theme_maintenance_page() is invoked. It is also used in theme_install_page() and theme_update_page() to keep all the variables consistent.
theme_color_scheme_form in modules/color/color.module
Theme the color form.
theme_dashboard in modules/dashboard/dashboard.module
Theme the entire dashboard.
theme_mediamosa_maintenance_browse_properties in sites/all/modules/mediamosa/maintenance/browse/mediamosa_maintenance_browse.inc
Our asset/collection detail view.
theme_mediamosa_simpletest_test_table in sites/all/modules/mediamosa/maintenance/browse/mediamosa_maintenance_browse_simpletest.inc
Reuse simpletest test table.
theme_profile_admin_overview in modules/profile/profile.admin.inc
Theme the profile field overview into a drag and drop enabled table.
theme_simpletest_test_table in modules/simpletest/simpletest.pages.inc
Theme the test list generated by simpletest_test_form() into a table.
theme_taxonomy_overview_terms in modules/taxonomy/taxonomy.admin.inc
Theme the terms overview as a sortable list of terms.
theme_update_report in modules/update/update.report.inc
Theme project status report.
update_manager_update_form in modules/update/update.manager.inc
Build the form for the update manager page to update existing projects.
user_init in modules/user/user.module
Implements hook_init().
_batch_page in includes/batch.inc
State-based dispatcher for the batch processing page.
_drupal_maintenance_theme in includes/theme.maintenance.inc
Sets up the theming system for site installs, updates and when the site is in maintenance mode. It also applies when the database is unavailable.
_drupal_theme_initialize in includes/theme.inc
Initialize the theme system given already loaded information. This function is useful to initialize a theme when no database is present.
_locale_translate_language_list in includes/locale.inc
List languages in search result table
_mediamosa_maintenance_status in sites/all/modules/mediamosa/maintenance/mediamosa_maintenance.module
Generate the status page.
_openid_user_login_form_alter in modules/openid/openid.module

Code

includes/common.inc, line 2658

<?php
function drupal_add_css($data = NULL, $options = NULL) {
  $css = &drupal_static(__FUNCTION__, array());

  // Construct the options, taking the defaults into consideration.
  if (isset($options)) {
    if (!is_array($options)) {
      $options = array('type' => $options);
    }
  }
  else {
    $options = array();
  }

  // Create an array of CSS files for each media type first, since each type needs to be served
  // to the browser differently.
  if (isset($data)) {
    $options += array(
      'type' => 'file',
      'weight' => CSS_DEFAULT,
      'media' => 'all',
      'preprocess' => TRUE,
      'data' => $data,
      'browsers' => array(),
    );
    $options['browsers'] += array(
      'IE' => TRUE,
      '!IE' => TRUE,
    );

    // Always add a tiny value to the weight, to conserve the insertion order.
    $options['weight'] += count($css) / 1000;

    // Add the data to the CSS array depending on the type.
    switch ($options['type']) {
      case 'inline':
        // For inline stylesheets, we don't want to use the $data as the array
        // key as $data could be a very long string of CSS.
        $css[] = $options;
        break;
      default:
        // Local and external files must keep their name as the associative key
        // so the same CSS file is not be added twice.
        $css[$data] = $options;
    }
  }

  return $css;
}
?>