BeforeRenderTextEvent
New in version 14.3.10 Feature: #111159 - Add a rendering event for f:render.text
The
Before is dispatched
by the
f:render.text ViewHelper
before it escapes a text field or processes it as rich text. A listener can
take over the rendering, for example to add controls for inline editing,
without overriding the ViewHelper.
The ViewHelper dispatches the event after it has validated the record, the value, and the type of the field. The event carries the record, the table, the full record type, the field name and its schema, the value, and the request, if there is one.
A listener provides the complete result with
Before.
The ViewHelper returns this result unchanged and skips its default rendering.
Later listeners are not called. The ViewHelper does not escape the result, so
the listener escapes or sanitizes the HTML itself. If no listener provides a
result, the ViewHelper renders the field as before.
With the argument optional, a field that does not exist renders as null,
and the event is not dispatched.
Example: Wrap text fields in an editable element in edit mode
The following listener wraps each single-line text field in an element for
inline editing, if the request has the attribute my_.
All other fields and requests render as before:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\EventListener;
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Core\Schema\Field\InputFieldType;
use TYPO3\CMS\Fluid\Event\BeforeRenderTextEvent;
use TYPO3Fluid\Fluid\Core\Parser\UnsafeHTMLString;
final readonly class EditableTextListener
{
#[AsEventListener(
identifier: 'my-extension/editable-text',
)]
public function __invoke(BeforeRenderTextEvent $event): void
{
if (!$event->getFieldSchema() instanceof InputFieldType) {
return;
}
if (!$event->getRequest()?->getAttribute('my_extension.edit_mode')) {
return;
}
// The listener escapes the value itself
$event->setRenderedText(new UnsafeHTMLString(
'<span class="editable">'
. htmlspecialchars($event->getValue())
. '</span>',
));
}
}
API of BeforeRenderTextEvent
-
final class
BeforeRenderTextEvent -
- Fully qualified name
-
\TYPO3\CMS\ Fluid\ Event\ Before Render Text Event
Allows listeners to take over rendering of a validated text field before escaping or rich-text processing. Providing a result stops propagation and skips the ViewHelper's default rendering.
getRecord()-
- Returns
-
\TYPO3\CMS\ Core\ Domain\ Record Interface |\ TYPO3\ CMS\ Extbase\ Domain Object\ Domain Object Interface
getFieldSchema()-
- Returns
-
\TYPO3\CMS\ Core\ Schema\ Field\ Input Field Type |\ TYPO3\ CMS\ Core\ Schema\ Field\ Text Field Type