Provide system.assets.beforeAddAsset event to modify asset attributes
Fixes octobercms/october#4611 (when combined with a PR to https://github.com/heathdutton/cloudflare) Related: octobercms/october#4092, octobercms/october#3841, octobercms/october#3839
This commit is contained in:
parent
fa002ce3dd
commit
e246427463
|
|
@ -407,10 +407,7 @@ class FileUpload extends FormWidgetBase
|
|||
protected function loadAssets()
|
||||
{
|
||||
$this->addCss('css/fileupload.css', 'core');
|
||||
$this->addJs('js/fileupload.js', [
|
||||
'build' => 'core',
|
||||
'cache' => 'false'
|
||||
]);
|
||||
$this->addJs('js/fileupload.js', 'core');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -159,10 +159,7 @@ class Form extends WidgetBase
|
|||
*/
|
||||
protected function loadAssets()
|
||||
{
|
||||
$this->addJs('js/october.form.js', [
|
||||
'build' => 'core',
|
||||
'cache' => 'false'
|
||||
]);
|
||||
$this->addJs('js/october.form.js', 'core');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
use Url;
|
||||
use Html;
|
||||
use File;
|
||||
use Event;
|
||||
use System\Models\Parameter;
|
||||
use System\Models\PluginVersion;
|
||||
use System\Classes\CombineAssets;
|
||||
|
|
@ -16,7 +17,6 @@ use System\Classes\CombineAssets;
|
|||
*/
|
||||
trait AssetMaker
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array Collection of assets to display in the layout.
|
||||
*/
|
||||
|
|
@ -127,16 +127,7 @@ trait AssetMaker
|
|||
|
||||
$jsPath = $this->getAssetScheme($jsPath);
|
||||
|
||||
// Prevent CloudFlare's Rocket Loader from breaking stuff
|
||||
// @see octobercms/october#4092, octobercms/october#3841, octobercms/october#3839
|
||||
if (isset($attributes['cache']) && $attributes['cache'] == 'false') {
|
||||
$attributes['data-cfasync'] = 'false';
|
||||
unset($attributes['cache']);
|
||||
}
|
||||
|
||||
if (!in_array($jsPath, $this->assets['js'])) {
|
||||
$this->assets['js'][] = ['path' => $jsPath, 'attributes' => $attributes];
|
||||
}
|
||||
$this->addAsset('js', $jsPath, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -164,9 +155,7 @@ trait AssetMaker
|
|||
|
||||
$cssPath = $this->getAssetScheme($cssPath);
|
||||
|
||||
if (!in_array($cssPath, $this->assets['css'])) {
|
||||
$this->assets['css'][] = ['path' => $cssPath, 'attributes' => $attributes];
|
||||
}
|
||||
$this->addAsset('css', $cssPath, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -190,8 +179,59 @@ trait AssetMaker
|
|||
|
||||
$rssPath = $this->getAssetScheme($rssPath);
|
||||
|
||||
if (!in_array($rssPath, $this->assets['rss'])) {
|
||||
$this->assets['rss'][] = ['path' => $rssPath, 'attributes' => $attributes];
|
||||
$this->addAsset('rss', $rssPath, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the provided asset to the internal asset collections
|
||||
*
|
||||
* @param string $type The type of the asset: 'js' || 'css' || 'rss'
|
||||
* @param string $path The path to the asset
|
||||
* @param array $attributes The attributes for the asset
|
||||
*/
|
||||
protected function addAsset(string $type, string $path, array $attributes)
|
||||
{
|
||||
if (!in_array($path, $this->assets[$type])) {
|
||||
/**
|
||||
* @event system.assets.beforeAddAsset
|
||||
* Provides an opportunity to inspect or modify an asset.
|
||||
*
|
||||
* The parameters provided are:
|
||||
* string `$type`: The type of the asset being added
|
||||
* string `$path`: The path to the asset being added
|
||||
* array `$attributes`: The array of attributes for the asset being added.
|
||||
*
|
||||
* All the parameters are provided by reference for modification.
|
||||
* This event is also a halting event, so returning false will prevent the
|
||||
* current asset from being added. Note that duplicates are filtered out
|
||||
* before the event is fired.
|
||||
*
|
||||
* Example usage:
|
||||
*
|
||||
* Event::listen('system.assets.beforeAddAsset', function (string $type, string $path, array $attributes) {
|
||||
* if (in_array($path, $blockedAssets)) {
|
||||
* return false;
|
||||
* }
|
||||
* });
|
||||
*
|
||||
* Or
|
||||
*
|
||||
* $this->bindEvent('assets.beforeAddAsset', function (string $type, string $path, array $attributes) {
|
||||
* $attributes['special_cdn_flag'] = false;
|
||||
* });
|
||||
*
|
||||
*/
|
||||
if (
|
||||
// Fire local event if exists
|
||||
(
|
||||
method_exists($this, 'fireEvent') &&
|
||||
($this->fireEvent('assets.beforeAddAsset', [&$type, &$path, &$attributes], true) !== false)
|
||||
) &&
|
||||
// Fire global event
|
||||
(Event::fire('system.assets.beforeAddAsset', [&$type, &$path, &$attributes], true) !== false)
|
||||
) {
|
||||
$this->assets[$type][] = ['path' => $path, 'attributes' => $attributes];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue