function AssetResolver::getFontAssets

Same name and namespace in other branches
  1. main core/lib/Drupal/Core/Asset/AssetResolver.php \Drupal\Core\Asset\AssetResolver::getFontAssets()

Returns the fonts for the current response's libraries.

Parameters

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

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

Return value

array An array of font assets.

Overrides AssetResolverInterface::getFontAssets

File

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

Class

AssetResolver
The default asset resolver.

Namespace

Drupal\Core\Asset

Code

public function getFontAssets(AttachedAssetsInterface $assets, ?LanguageInterface $language = NULL) : array {
  if (!$assets->getLibraries()) {
    return [];
  }
  // Get the complete list of libraries to load including dependencies.
  $libraries_to_load = $this->getLibrariesToLoad($assets, 'fonts');
  if (!$libraries_to_load) {
    return [];
  }
  if (!isset($language)) {
    $language = $this->languageManager
      ->getCurrentLanguage();
  }
  // Add the active theme name to the cache key since active themes may
  // implement hook_library_info_alter().
  $active_theme = $this->themeManager
    ->getActiveTheme()
    ->getName();
  $cid = 'fonts:' . $active_theme . ':' . $language->getId() . Crypt::hashBase64(serialize($libraries_to_load));
  if ($cached = $this->cache
    ->get($cid)) {
    return $cached->data;
  }
  $fonts = [];
  foreach ($libraries_to_load as $library) {
    [$extension, $name] = explode('/', $library, 2);
    $definition = $this->libraryDiscovery
      ->getLibraryByName($extension, $name);
    foreach ($definition['fonts'] as $font) {
      $fonts[] = $font;
    }
  }
  $this->cache
    ->set($cid, $fonts, CacheBackendInterface::CACHE_PERMANENT, [
    'library_info',
  ]);
  return $fonts;
}

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