function AssetResolver::getCssAssets

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Asset/AssetResolver.php \Drupal\Core\Asset\AssetResolver::getCssAssets()
  2. 8.9.x core/lib/Drupal/Core/Asset/AssetResolver.php \Drupal\Core\Asset\AssetResolver::getCssAssets()
  3. 11.x core/lib/Drupal/Core/Asset/AssetResolver.php \Drupal\Core\Asset\AssetResolver::getCssAssets()

Returns the CSS assets for the current response's libraries.

It returns the CSS assets in order, according to the SMACSS categories specified in the assets' weights:

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/olivero/system-menus.css would replace modules/system/system-menus.css. This allows themes to override complete CSS files, rather than specific selectors, when necessary.

Also invokes hook_css_alter(), to allow CSS assets to be altered.

Parameters

\Drupal\Core\Asset\AttachedAssetsInterface $assets: The assets attached to the current response.

bool $optimize: Whether to apply the CSS asset collection optimizer, to return an optimized CSS asset collection rather than an unoptimized one.

\Drupal\Core\Language\LanguageInterface $language: (optional) The interface language the assets will be rendered with.

Return value

array A (possibly optimized) collection of CSS assets.

Overrides AssetResolverInterface::getCssAssets

File

core/lib/Drupal/Core/Asset/AssetResolver.php, line 124

Class

AssetResolver
The default asset resolver.

Namespace

Drupal\Core\Asset

Code

public function getCssAssets(AttachedAssetsInterface $assets, $optimize, ?LanguageInterface $language = NULL) {
  if (!$assets->getLibraries()) {
    return [];
  }
  $libraries_to_load = $this->getLibrariesToLoad($assets);
  foreach ($libraries_to_load as $key => $library) {
    [
      $extension,
      $name,
    ] = explode('/', $library, 2);
    $definition = $this->libraryDiscovery
      ->getLibraryByName($extension, $name);
    if (empty($definition['css'])) {
      unset($libraries_to_load[$key]);
    }
  }
  $libraries_to_load = array_values($libraries_to_load);
  if (!$libraries_to_load) {
    return [];
  }
  if (!isset($language)) {
    $language = $this->languageManager
      ->getCurrentLanguage();
  }
  $theme_info = $this->themeManager
    ->getActiveTheme();
  // Add the theme name to the cache key since themes may implement
  // hook_library_info_alter().
  $cid = 'css:' . $theme_info->getName() . ':' . $language->getId() . Crypt::hashBase64(serialize($libraries_to_load)) . (int) $optimize;
  if ($cached = $this->cache
    ->get($cid)) {
    return $cached->data;
  }
  $css = [];
  $default_options = [
    'type' => 'file',
    'group' => CSS_AGGREGATE_DEFAULT,
    'weight' => 0,
    'media' => 'all',
    'preprocess' => TRUE,
  ];
  foreach ($libraries_to_load as $key => $library) {
    [
      $extension,
      $name,
    ] = explode('/', $library, 2);
    $definition = $this->libraryDiscovery
      ->getLibraryByName($extension, $name);
    foreach ($definition['css'] as $options) {
      $options += $default_options;
      // Copy the asset library license information to each file.
      $options['license'] = $definition['license'];
      // Files with a query string cannot be preprocessed.
      if ($options['type'] === 'file' && $options['preprocess'] && str_contains($options['data'], '?')) {
        $options['preprocess'] = FALSE;
      }
      // Always add a tiny value to the weight, to conserve the insertion
      // order.
      $options['weight'] += count($css) / 30000;
      // CSS files are being keyed by the full path.
      $css[$options['data']] = $options;
    }
  }
  // Allow modules and themes to alter the CSS assets.
  $this->moduleHandler
    ->alter('css', $css, $assets, $language);
  $this->themeManager
    ->alter('css', $css, $assets, $language);
  if (!empty($css)) {
    // Sort CSS items, so that they appear in the correct order.
    uasort($css, [
      static::class,
      'sort',
    ]);
    if ($optimize) {
      $css = \Drupal::service('asset.css.collection_optimizer')->optimize($css, array_values($libraries_to_load), $language);
    }
  }
  $this->cache
    ->set($cid, $css, CacheBackendInterface::CACHE_PERMANENT, [
    'library_info',
  ]);
  return $css;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.