function AssetResolver::getLibrariesToLoad

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

Returns the libraries that need to be loaded.

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

$assets = new AttachedAssets();
$assets->setLibraries([
  'core/a',
  'core/b',
  'core/c',
]);
$assets->setAlreadyLoadedLibraries([
  'core/c',
]);
$resolver->getLibrariesToLoad($assets, 'js') === [
  'core/a',
  'core/b',
  'core/d',
];

The attached assets tend to be in the order that libraries were attached during a request. To minimize the number of unique aggregated asset URLs and files, we normalize the list by filtering out libraries that don't include the asset type being built as well as ensuring a reliable order of the libraries based on their dependencies.

Parameters

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

string|null $asset_type: The asset type to load.

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.

3 calls to AssetResolver::getLibrariesToLoad()
AssetResolver::getCssAssets in core/lib/Drupal/Core/Asset/AssetResolver.php
Returns the CSS assets for the current response's libraries.
AssetResolver::getJsAssets in core/lib/Drupal/Core/Asset/AssetResolver.php
Returns the JavaScript assets for the current response's libraries.
AssetResolver::getJsSettingsAssets in core/lib/Drupal/Core/Asset/AssetResolver.php
Returns the JavaScript settings assets for this response's libraries.

File

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

Class

AssetResolver
The default asset resolver.

Namespace

Drupal\Core\Asset

Code

protected function getLibrariesToLoad(AttachedAssetsInterface $assets, ?string $asset_type = NULL) {
  // @see Drupal\FunctionalTests\Core\Asset\AssetOptimizationTestUmami
  // @todo https://www.drupal.org/project/drupal/issues/1945262
  $libraries_to_load = array_diff($this->libraryDependencyResolver
    ->getLibrariesWithDependencies($assets->getLibraries()), $this->libraryDependencyResolver
    ->getLibrariesWithDependencies($assets->getAlreadyLoadedLibraries()));
  if ($asset_type) {
    $libraries_to_load = $this->filterLibrariesByType($libraries_to_load, $asset_type);
  }
  // We now have a complete list of libraries requested. However, this list
  // could be in any order depending on when libraries were attached during
  // the page request, which can result in different file contents and URLs
  // even for an otherwise identical set of libraries. To ensure that any
  // particular set of libraries results in the same aggregate URL, sort the
  // libraries, then generate the minimum representative set again.
  sort($libraries_to_load);
  $minimum_libraries = $this->libraryDependencyResolver
    ->getMinimalRepresentativeSubset($libraries_to_load);
  $libraries_to_load = array_diff($this->libraryDependencyResolver
    ->getLibrariesWithDependencies($minimum_libraries), $this->libraryDependencyResolver
    ->getLibrariesWithDependencies($assets->getAlreadyLoadedLibraries()));
  // Now remove any libraries without the relevant asset type again, since
  // they have been brought back in via dependencies.
  if ($asset_type) {
    $libraries_to_load = $this->filterLibrariesByType($libraries_to_load, $asset_type);
  }
  return $libraries_to_load;
}

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