class AssetResolver implements AssetResolverInterface (View source)

The default asset resolver.

Properties

protected LibraryDiscoveryInterface $libraryDiscovery

The library discovery service.

protected LibraryDependencyResolverInterface $libraryDependencyResolver

The library dependency resolver.

protected ModuleHandlerInterface $moduleHandler

The module handler.

protected ThemeManagerInterface $themeManager

The theme manager.

protected LanguageManagerInterface $languageManager

The language manager.

protected CacheBackendInterface $cache

The cache backend.

Methods

__construct(LibraryDiscoveryInterface $library_discovery, LibraryDependencyResolverInterface $library_dependency_resolver, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager, LanguageManagerInterface $language_manager, CacheBackendInterface $cache)

Constructs a new AssetResolver instance.

string[]
getLibrariesToLoad(AttachedAssetsInterface $assets)

Returns the libraries that need to be loaded.

array
getCssAssets(AttachedAssetsInterface $assets, bool $optimize)

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

array
getJsSettingsAssets(AttachedAssetsInterface $assets)

Returns the JavaScript settings assets for this response's libraries.

array
getJsAssets(AttachedAssetsInterface $assets, bool $optimize)

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

static int
sort($a, $b)

Sorts CSS and JavaScript resources.

Details

__construct(LibraryDiscoveryInterface $library_discovery, LibraryDependencyResolverInterface $library_dependency_resolver, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager, LanguageManagerInterface $language_manager, CacheBackendInterface $cache)

Constructs a new AssetResolver instance.

Parameters

LibraryDiscoveryInterface $library_discovery

The library discovery service.

LibraryDependencyResolverInterface $library_dependency_resolver

The library dependency resolver.

ModuleHandlerInterface $module_handler

The module handler.

ThemeManagerInterface $theme_manager

The theme manager.

LanguageManagerInterface $language_manager

The language manager.

CacheBackendInterface $cache

The cache backend.

protected string[] getLibrariesToLoad(AttachedAssetsInterface $assets)

Returns the libraries that need to be loaded.

For example, with core/a depending on core/c and core/b on core/d:

Parameters

AttachedAssetsInterface $assets

The assets attached to the current response.

Return Value

string[]

A list of libraries and their dependencies, in the order they should be loaded, excluding any libraries that have already been loaded.

array getCssAssets(AttachedAssetsInterface $assets, bool $optimize)

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:

  • CSS_BASE
  • CSS_LAYOUT
  • CSS_COMPONENT
  • CSS_STATE
  • CSS_THEME

Parameters

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.

Return Value

array

A (possibly optimized) collection of CSS assets.

protected array getJsSettingsAssets(AttachedAssetsInterface $assets)

Returns the JavaScript settings assets for this response's libraries.

Gathers all drupalSettings from all libraries in the attached assets collection and merges them.

Parameters

AttachedAssetsInterface $assets

The assets attached to the current response.

Return Value

array

A (possibly optimized) collection of JavaScript assets.

array getJsAssets(AttachedAssetsInterface $assets, bool $optimize)

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

References to JavaScript files are placed in a certain order: first, all 'core' files, then all 'module' and finally all 'theme' JavaScript files are added to the page. Then, all settings are output, followed by 'inline' JavaScript code. If running update.php, all preprocessing is disabled.

Note that hook_js_alter(&$javascript) is called during this function call to allow alterations of the JavaScript during its presentation. The correct way to add JavaScript during hook_js_alter() is to add another element to the $javascript array, deriving from drupal_js_defaults(). See locale_js_alter() for an example of this.

Parameters

AttachedAssetsInterface $assets

The assets attached to the current response. Note that this object is modified to reflect the final JavaScript settings assets.

bool $optimize

Whether to apply the JavaScript asset collection optimizer, to return optimized JavaScript asset collections rather than an unoptimized ones.

Return Value

array

A nested array containing 2 values:

  • at index zero: the (possibly optimized) collection of JavaScript assets for the top of the page
  • at index one: the (possibly optimized) collection of JavaScript assets for the bottom of the page

static int sort($a, $b)

Sorts CSS and JavaScript resources.

This sort order helps optimize front-end performance while providing modules and themes with the necessary control for ordering the CSS and JavaScript appearing on a page.

Parameters

$a

First item for comparison. The compared items should be associative arrays of member items.

$b

Second item for comparison.

Return Value

int