Which Magento 2 core function creates the url for the static folder files including css, js and images
-
17-01-2021 - |
Question
I want to add a parameter at the end of the static content urls, but i am unable to find a global method where i can make a change and it starts reflecting on the all static content urls.
My file and code where i tried making the changes:
File path: vendor/magento/framework/View/Asset/Repository.php
code that i tried to modify:
public function createAsset($fileId, array $params = [])
{
$this->updateDesignParams($params);
list($module, $filePath) = self::extractModule($fileId);
if (!$module && $params['module']) {
$module = $params['module'];
}
if (!isset($params['publish'])) {
$map = $this->getRepositoryFilesMap($fileId, $params);
if ($map) {
$params = array_replace($params, $map);
}
}
$isSecure = isset($params['_secure']) ? (bool) $params['_secure'] : null;
$themePath = isset($params['theme']) ? $params['theme'] : $this->design->getThemePath($params['themeModel']);
$context = $this->getFallbackContext(
UrlInterface::URL_TYPE_STATIC,
$isSecure,
$params['area'],
$themePath,
$params['locale']
);
return $this->fileFactory->create(
[
'source' => $this->assetSource,
'context' => $context,
'filePath' => $filePath."?mytest='value'",
'module' => $module,
'contentType' => $this->assetSource->getContentType($filePath)
]
);
}
Where "?mytest='value'"
, is my custom parameter that i want to pass globally for all the static files that load from my pub/static folder through web url.
Example relative url that i want to load for all the static content urls: en_US/Magento_Ui/js/lib/knockout/bindings/bootstrap.js?mytest='value'
Solution
i found the answer for my above question.
URLs that are created during the page load time are create from the file:
Magento\Framework\View\Asset\File
In the method:
getUrl()
Requirejs dependency urls are loaded from the requirejs-config.js
file.
I hope this answer helps others looking to modify functionality related to static urls.