BeforeStylesheetsRenderingEvent¶
New in version 10.4.
This event is fired once before \TYPO3\CMS\Core\Page\AssetRenderer::render[Inline]Stylesheets
renders the output.
The AssetRenderer
is amended by two events which allow post-processing of
AssetCollector
assets.
\TYPO3\CMS\Core\Page\Event\BeforeJavaScriptsRenderingEvent
\TYPO3\CMS\Core\Page\Event\BeforeStylesheetsRenderingEvent
The events are fired once for every combination of
inline
/priority
before the corresponding section of JS/CSS assets
is rendered by the AssetRenderer.
API¶
- getAssetCollector()
| ReturnType:
AssetCollector
|- isInline()
| ReturnType: bool |
- isPriority()
| ReturnType: bool |
To make the events easier to use, the AssetCollector::get*()
methods
have an optional parameter ?bool $priority = null
which when given a
boolean only returns assets of the given priority.
Note
post-processing functionality for assets registered via
TypoScript page.include...
or the PageRenderer::add*()
functions are still provided by these hooks:
$GLOBALS['TYPO3_CONF_VARS']['FE']['cssCompressHandler']
$GLOBALS['TYPO3_CONF_VARS']['FE']['jsCompressHandler']
$GLOBALS['TYPO3_CONF_VARS']['FE']['cssConcatenateHandler']
$GLOBALS['TYPO3_CONF_VARS']['FE']['jsConcatenateHandler']
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_pagerenderer.php']['render-preProcess']
Assets registered with the AssetCollector (and output through the AssetRenderer) are not included in those.
Example¶
As an example let’s make sure jQuery is included in a specific version and from a CDN.
Register our listeners
Configuration/Services.yaml
services: MyVendor\MyExt\EventListener\AssetRenderer\LibraryVersion: tags: - name: event.listener identifier: 'myExt/LibraryVersion' event: TYPO3\CMS\Core\Page\Event\BeforeJavaScriptsRenderingEvent
Implement Listener to enforce a library version or CDN URI
namespace MyVendor\MyExt\EventListener\AssetRenderer; use TYPO3\CMS\Core\Page\Event\BeforeJavaScriptsRenderingEvent; /** * If a library has been registered, it is made sure that it is loaded * from the given URI */ class LibraryVersion { protected $libraries = [ 'jquery' => 'https://code.jquery.com/jquery-3.4.1.min.js', ]; public function __invoke(BeforeJavaScriptsRenderingEvent $event): void { if ($event->isInline()) { return; } foreach ($this->libraries as $library => $source) { $asset = $event->getAssetCollector()->getJavaScripts($event->isPriority()) // if it was already registered if ($asset[$library] ?? false) { // we set our authoritative version $event->getAssetCollector()->addJavaScript($library, $source); } } } }