EvaluateModifierFunctionEvent¶
New in version 12.0: This event is a substitution of the
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsparser.php']['preParseFunc']
hook.
The PSR-14 event
\TYPO3\CMS\Core\TypoScript\AST\Event\EvaluateModifierFunctionEvent
allows custom TypoScript functions using the :=
operator.
Example¶
A simple TypoScript example looks like this:
someIdentifier = originalValue
someIdentifier := myModifierFunction(myFunctionArgument)
To implement myModifierFunction
, an extension needs to register
an event listener in an extension's Services.yaml
:
services:
MyVendor\MyExtension\TypoScript\EventListener\MyEventListener:
tags:
- name: event.listener
identifier: 'my-extension/evaluate-modifier-function'
The corresponding event listener class could look like this:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\TypoScript\EventListener;
use TYPO3\CMS\Core\TypoScript\AST\Event\EvaluateModifierFunctionEvent;
final class MyEventListener
{
public function __invoke(EvaluateModifierFunctionEvent $event): void
{
if ($event->getFunctionName() === 'myModifierFunction') {
$originalValue = $event->getOriginalValue();
$functionArgument = $event->getFunctionArgument();
// Manipulate values and set new value
$event->setValue($originalValue . ' example ' . $functionArgument);
}
}
}
API¶
- class TYPO3\CMS\Core\TypoScript\AST\Event\EvaluateModifierFunctionEvent¶
Listeners to this event are able to implement own ":=" TypoScript modifier functions, example:
foo = myOriginalValue foo := myNewFunction(myFunctionArgument)
Listeners should take care function names can not overlap with function names from other extensions and should thus namespace, example naming: "extNewsSortFunction()"
- getFunctionName()¶
The function name, for example "extNewsSortFunction" when using "foo := extNewsSortFunction()"
- Return type
string
- getFunctionArgument()¶
Optional function argument, for example "myArgument" when using "foo := extNewsSortFunction(myArgument)"
- Return type
string
- getOriginalValue()¶
Original / current value, for example "fooValue" when using: foo = fooValue foo := extNewsSortFunction(myArgument)
- Return type
string
- setValue(string $value)¶
Set the updated value calculated by a listener.
Note you can not set to null to "unset", since getValue() falls back to originalValue in this case. Set to empty string instead for this edge case.
- Parameters
$value (
string
) -- the value
- getValue()¶
Used by AstBuilder to fetch the updated value, falls back to given original value.
Can be used by Listeners to see if a previous listener changed the value already by comparing with getOriginalValue().
- Return type
string