RenderComponentEvent
New in version 14.1
The
Render can be used to alter or
replace the rendering of Fluid components.
There are three possible use cases:
- Fully take over the rendering of components by filling the
$renderedwithContent $event->set. The first event listener that does this skips all following event listeners.Rendered Content () - Provide additional arguments (variables in the component template) or slots to
the component with
$event->set/Arguments () $event->set.Slots () - Execute additional code that doesn't influence the component rendering directly, for example adding certain frontend assets to the page automatically.
Example
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\EventListener;
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Core\Page\AssetCollector;
use TYPO3\CMS\Fluid\Event\RenderComponentEvent;
#[AsEventListener]
final readonly class RenderComponentListener
{
public function __construct(private AssetCollector $assetCollector) {}
public function __invoke(RenderComponentEvent $event): void
{
// Add bundled component's CSS if a component is used on the page
if ($event->getComponentCollection()->getNamespace() === 'MyVendor\\MyExtension\\Components') {
$this->assetCollector->addStyleSheet(
'componentsBundle',
'EXT:my_extension/Resources/Public/ComponentsBundle.css',
);
}
}
}
API
- class RenderComponentEvent
-
- Fully qualified name
-
\TYPO3\
CMS\ Fluid\ Event\ Render Component Event
Event to modify/replace the rendering behavior of Fluid components.
This can be used both to take over the whole rendering (by filling the $renderedContent property) and to introduce side effects while leaving the default rendering intact, like for embedding related frontent assets automatically. Also, both arguments and slots can be altered.
The event chain stops with the first listener that takes over the rendering completely. If no listener stops the chain, Fluid's default component rendering will be triggered afterwards.
Note that the provided parentRenderingContext must not be used to render child templates as this might have side effects on the parent template. Instead, a new rendering context (or a clone of the parent) must be used.