From e246427463ac5edcc6b7115b33838a4367a4f9a9 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Wed, 9 Oct 2019 11:51:06 -0600 Subject: [PATCH] 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 --- modules/backend/formwidgets/FileUpload.php | 5 +- modules/backend/widgets/Form.php | 5 +- modules/system/traits/AssetMaker.php | 72 +++++++++++++++++----- 3 files changed, 58 insertions(+), 24 deletions(-) diff --git a/modules/backend/formwidgets/FileUpload.php b/modules/backend/formwidgets/FileUpload.php index 0a8f5d1ba..89e2d3e57 100644 --- a/modules/backend/formwidgets/FileUpload.php +++ b/modules/backend/formwidgets/FileUpload.php @@ -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'); } /** diff --git a/modules/backend/widgets/Form.php b/modules/backend/widgets/Form.php index 17e630b96..c16e75bdd 100644 --- a/modules/backend/widgets/Form.php +++ b/modules/backend/widgets/Form.php @@ -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'); } /** diff --git a/modules/system/traits/AssetMaker.php b/modules/system/traits/AssetMaker.php index 673e53596..4918872e7 100644 --- a/modules/system/traits/AssetMaker.php +++ b/modules/system/traits/AssetMaker.php @@ -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]; + } } }