Skip to content

civi.token.render

Summary

This Symfony event is dispatched after a message template has been rendered and all tokens replaced. It allows developers to perform post-processing or cleanup on the rendered output string.

Warning

The render event is designed primarily for simple post-processing or text adjustment (such as formatting cleanup). It can be difficult to perform secure, robust modifications inside this event; it primarily exists to support legacy compatibility patterns.

Definition

This event is dispatched via the Symfony EventDispatcher.

public static function onTokenRender(\Civi\Token\Event\TokenRenderEvent $event): void

Parameters

  • \Civi\Token\Event\TokenRenderEvent $event

Event Methods

  • getTokenProcessor(): \Civi\Token\TokenProcessor - Returns the token processor instance.

Event Properties

  • $event->context: array|\ArrayAccess - Context data for token rendering.
  • $event->message: array|\ArrayAccess - The original message template array (e.g. containing 'string' and 'format').
  • $event->row: \Civi\Token\TokenRow - The row record representing the entity instance (e.g. contact) being rendered.
  • $event->string: string - A reference to the final rendered string. Alterable.

Example

use Civi\Token\Event\TokenRenderEvent;

public static function onTokenRender(TokenRenderEvent $event): void {
  // Post-process the rendered HTML or text string
  if ($event->message['format'] === 'text/html') {
    // Strip double-newlines or perform minor regex tidy-up
    $event->string = preg_replace('/<p>&nbsp;<\/p>/', '', $event->string);
  }
}