You are here

public function CssCollectionRenderer::render in Critical CSS 8

Renders an asset collection.

Parameters

array $assets: An asset collection.

Return value

array A render array to render the asset collection.

Overrides AssetCollectionRendererInterface::render

File

src/Asset/CssCollectionRenderer.php, line 68

Class

CssCollectionRenderer
Decorates the CSS collection renderer service, adds Critical CSS.

Namespace

Drupal\critical_css\Asset

Code

public function render(array $css_assets) {

  // Fixes https://www.drupal.org/project/critical_css/issues/2982651
  if (empty($css_assets)) {
    return [];
  }
  $css_assets = $this->cssCollectionRenderer
    ->render($css_assets);
  $config = $this->configFactory
    ->get('critical_css.settings');
  if (!$this->criticalCssProvider
    ->isEnabled() || $this->criticalCssProvider
    ->isAlreadyProcessed()) {
    return $css_assets;
  }
  $new_css_assets = [];

  // Add critical CSS asset. It will be added if a critical CSS file is found
  // or if Twig debug is enabled.
  $criticalCssAsset = $this
    ->getCriticalCssAsset();
  if ($criticalCssAsset) {
    $new_css_assets[] = $criticalCssAsset;
  }

  // If a critical CSS is found, make other CSS files asynchronous.
  $criticalCss = $this->criticalCssProvider
    ->getCriticalCss();
  if ($criticalCss) {
    $asyncAssets = $this
      ->makeAssetsAsync($css_assets);
    $new_css_assets = array_merge($new_css_assets, $asyncAssets);
  }
  else {
    $new_css_assets = array_merge($new_css_assets, $css_assets);
  }
  return $new_css_assets;
}