RenderComponentEvent 

New in version 14.1

The RenderComponentEvent can be used to alter or replace the rendering of Fluid components. There are three possible use cases:

  1. Fully take over the rendering of components by filling the $renderedContent with $event->setRenderedContent(). The first event listener that does this skips all following event listeners.
  2. Provide additional arguments (variables in the component template) or slots to the component with $event->setArguments()/ $event->setSlots().
  3. Execute additional code that doesn't influence the component rendering directly, for example adding certain frontend assets to the page automatically.

Example 

EXT:my_extension/Classes/EventListener/RenderComponentListener.php
<?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',
            );
        }
    }
}
Copied!

API 

class RenderComponentEvent
Fully qualified name
\TYPO3\CMS\Fluid\Event\RenderComponentEvent

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.

getComponentCollection ( )
getViewHelperName ( )
Returns
string
getParentRenderingContext ( )
Returns
TYPO3FluidFluidCoreRenderingRenderingContextInterface
getRequest ( )
Returns
?PsrHttpMessageServerRequestInterface
getArguments ( )
Returns
array<string,mixed>
setArguments ( array $arguments)
param $arguments

the arguments

getSlots ( )
Returns
array<string,callable>
setSlots ( array $slots)
param $slots

the slots

setRenderedComponent ( string $renderedComponent)
param $renderedComponent

the renderedComponent

getRenderedComponent ( )
Returns
?string
isPropagationStopped ( )
Returns
bool