function JsAssetController::getGroups
Same name and namespace in other branches
- 11.x core/modules/system/src/Controller/JsAssetController.php \Drupal\system\Controller\JsAssetController::getGroups()
- 10 core/modules/system/src/Controller/JsAssetController.php \Drupal\system\Controller\JsAssetController::getGroups()
Get grouped assets.
Parameters
\Drupal\Core\Asset\AttachedAssetsInterface $attached_assets: The attached assets.
\Symfony\Component\HttpFoundation\Request $request: The current request.
Return value
array The grouped assets.
Overrides AssetControllerBase::getGroups
File
-
core/
modules/ system/ src/ Controller/ JsAssetController.php, line 47
Class
- JsAssetController
- Defines a controller to serve Javascript aggregates.
Namespace
Drupal\system\ControllerCode
protected function getGroups(AttachedAssetsInterface $attached_assets, Request $request) : array {
// The header and footer scripts are two distinct sets of asset groups. The
// $group_key is not sufficient to find the group, we also need to locate it
// within either the header or footer set.
$language = $this->languageManager()
->getLanguage($request->query
->get('language'));
if ($request->query
->has('libraries')) {
[$js_assets_header, $js_assets_footer] = $this->assetResolver
->getJsAssets($attached_assets, FALSE, $language, FALSE);
// When the libraries query argument is set, the aggregate contains every
// file from each of the listed libraries. However, which scope an
// individual library appears in is determined by the HTML page request as
// well as the library definition, depending on the dependencies between
// libraries. This means we must ignore the scope when collecting the
// libraries and get them from both the header and footer in order.
// When building the array, also remove the scope from each asset so that
// it is ignored when calculating the hash, as in
// JsCollectionOptimizerLazy::optimize().
$assets = [];
unset($js_assets_header['drupalSettings'], $js_assets_footer['drupalSettings']);
foreach ($js_assets_header as $key => $asset) {
unset($asset['scope']);
$assets[$key] = $asset;
}
foreach ($js_assets_footer as $key => $asset) {
unset($asset['scope']);
$assets[$key] = $asset;
}
}
else {
$scope = $request->query
->get('scope');
if (!isset($scope)) {
throw new BadRequestHttpException('The URL must have a scope query argument.');
}
[$js_assets_header, $js_assets_footer] = $this->assetResolver
->getJsAssets($attached_assets, FALSE, $language);
$assets = $scope === 'header' ? $js_assets_header : $js_assets_footer;
unset($assets['drupalSettings']);
}
return $this->grouper
->group($assets);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.