function drupal_get_css

Returns a themed representation of all stylesheets to attach 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/bartik/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.

$skip_alter: (optional) If set to TRUE, this function skips calling drupal_alter() on $css, useful when the calling function passes a $css array that has already been altered.

Return value

A string of XHTML CSS tags.

See also

drupal_add_css()

16 calls to drupal_get_css()
AjaxResponse::ajaxRender in drupal/core/lib/Drupal/Core/Ajax/AjaxResponse.php
Prepares the AJAX commands for sending back to the client.
ajax_render in drupal/core/includes/ajax.inc
Renders a commands array into JSON.
CascadingStylesheetsTest::testAlter in drupal/core/modules/system/lib/Drupal/system/Tests/Common/CascadingStylesheetsTest.php
Tests Locale module's CSS Alter to include RTL overrides.
CascadingStylesheetsTest::testRenderExternal in drupal/core/modules/system/lib/Drupal/system/Tests/Common/CascadingStylesheetsTest.php
Tests rendering an external stylesheet.
CascadingStylesheetsTest::testRenderFile in drupal/core/modules/system/lib/Drupal/system/Tests/Common/CascadingStylesheetsTest.php
Tests rendering the stylesheets.

... See full list

File

drupal/core/includes/common.inc, line 1878
Common functions that many Drupal modules will need to reference.

Code

function drupal_get_css($css = NULL, $skip_alter = FALSE) {
  global $theme_info;
  if (!isset($css)) {
    $css = drupal_add_css();
  }

  // Allow modules and themes to alter the CSS items.
  if (!$skip_alter) {
    drupal_alter('css', $css);
  }

  // Sort CSS items, so that they appear in the correct order.
  uasort($css, 'drupal_sort_css_js');

  // Allow themes to remove CSS files by basename.
  if (!empty($theme_info->stylesheets_remove)) {
    foreach ($css as $key => $options) {
      if (isset($options['basename']) && isset($theme_info->stylesheets_remove[$options['basename']])) {
        unset($css[$key]);
      }
    }
  }

  // Allow themes to conditionally override CSS files by basename.
  if (!empty($theme_info->stylesheets_override)) {
    foreach ($css as $key => $options) {
      if (isset($options['basename']) && isset($theme_info->stylesheets_override[$options['basename']])) {
        $css[$key]['data'] = $theme_info->stylesheets_override[$options['basename']];
      }
    }
  }

  // Render the HTML needed to load the CSS.
  $styles = array(
    '#type' => 'styles',
    '#items' => $css,
  );
  if (!empty($setting)) {
    $styles['#attached']['js'][] = array(
      'type' => 'setting',
      'data' => $setting,
    );
  }
  return drupal_render($styles);
}