BeforeRenderTextEvent 

The BeforeRenderTextEvent 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 BeforeRenderTextEvent->setRenderedText() . 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_extension.edit_mode. All other fields and requests render as before:

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

API of BeforeRenderTextEvent 

final class BeforeRenderTextEvent
Fully qualified name
\TYPO3\CMS\Fluid\Event\BeforeRenderTextEvent

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\RecordInterface|\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
getTable()
Returns
string
getFullType()
Returns
string
getFieldName()
Returns
string
getFieldSchema()
Returns
\TYPO3\CMS\Core\Schema\Field\InputFieldType|\TYPO3\CMS\Core\Schema\Field\TextFieldType
getValue()
Returns
string
getRequest()
Returns
?\Psr\Http\Message\ServerRequestInterface
getRenderedText()
Returns
?\TYPO3Fluid\Fluid\Core\Parser\UnsafeHTML
setRenderedText(\TYPO3Fluid\Fluid\Core\Parser\UnsafeHTML $renderedText)

The result must contain appropriately escaped or sanitized HTML.

param $renderedText

the renderedText

isPropagationStopped()
Returns
bool