Expression language
JMS serializer, which is used by t3api, supports Symfony expression language in serialization metadata. Expressions can be used to:
- compute a virtual property (
expkey ofvirtual_in YAML metadata),properties - exclude or expose a property conditionally (
exclude_/if expose_in YAML, or t3api annotationif @T3api\),Serializer\ Exclude (if="...") - exclude a whole class conditionally (
exclude_on class level in YAML).if
Important
T3api builds serializer metadata only from its own annotations (\Source) and YAML metadata. JMS annotations (@JMS\) in model classes are ignored, so use YAML for everything which t3api annotations do not cover (e.g. exp, expose_).
Variables
Variables are passed by JMS serializer, so they are different from the variables available in security expressions:
object- the object being serialized.context- JMS serialization context (\JMS\). Note that it is not the TYPO3Serializer\ Serialization Context Contextobject which is available ascontextin security expressions.property_- metadata of the property being serialized (inmetadata expand propertyexclude_/if expose_).if class_- metadata of the class (in class levelmetadata exclude_only).if
frontend., backend. and other security variables are not available in serialization expressions.
Context attributes
context. gives access to attributes of the serialization context. Following attributes are set by t3api for every operation:
groups- serialization groups of the operation (fromnormalization).Context - any other key set in
normalizationof the operation.Context TYPO3_,SITE_ URL TYPO3_,REQUEST_ HOST TYPO3_,REQUEST_ URL TYPO3_,HOST_ ONLY TYPO3_,PORT TYPO3_,SSL TYPO3_,PROXY TYPO3_,SITE_ PATH TYPO3_,SITE_ SCRIPT TYPO3_,REQUEST_ SCRIPT TYPO3_,REQUEST_ DIR TYPO3_- values ofDOCUMENT_ ROOT Normalizedof the current request (Params $_when the request has none, e.g. on CLI).SERVER TYPO3_andSSL TYPO3_(request comes through a configured reverse proxy) are booleans,PROXY TYPO3_is empty when the host has no port.PORT
Use context. before reading an attribute which may not exist - context. of a missing attribute triggers an error.
Functions
force_- returnsabsolute_ url (url, fallback Host) urlunchanged if it is already absolute (has a scheme or starts with//), otherwise prefixes it withfallback.Host
T3api uses it to expose absolute URL of files (EXT:):
TYPO3\CMS\Core\Resource\AbstractFile:
virtual_properties:
absolutePublicUrl:
type: string
exp: force_absolute_url(object.getPublicUrl(), context.getAttribute('TYPO3_SITE_URL'))
Functions of TYPO3 core default provider (like, compat, date, feature, traverse) and functions from your own providers registered for t3api context are available as well.
Examples
Virtual property computed from the object (YAML metadata):
Vendor\Users\Domain\Model\User:
virtual_properties:
fullName:
exp: "object.getFirstName() ~ ' ' ~ object.getLastName()"
type: string
groups: [api_get_item_user]
Property returned only if a custom attribute is set in normalization of the operation:
use SourceBroker\T3api\Annotation as T3api;
/**
* @T3api\ApiResource(
* itemOperations={
* "get"={
* "path"="/users/{id}",
* },
* "get_full"={
* "path"="/users/{id}/full",
* "normalizationContext"={
* "includeEmail"=true
* },
* },
* },
* )
*/
class User extends \TYPO3\CMS\Extbase\Domain\Model\FrontendUser
{
/**
* @T3api\Serializer\Exclude(if="!context.hasAttribute('includeEmail')")
*/
protected $email = '';
}
The same in YAML metadata (see Yaml metadata):
Vendor\Users\Domain\Model\User:
properties:
email:
exclude_if: "!context.hasAttribute('includeEmail')"
To make a property depend on the current user (e.g. return email only to backend admins), set a context attribute in After (see Events) and check it in the expression:
use SourceBroker\T3api\Event\AfterCreateContextForOperationEvent;
use TYPO3\CMS\Core\Context\Context;
class AddIsAdminContextAttributeEventListener
{
public function __construct(private readonly Context $context) {}
public function __invoke(AfterCreateContextForOperationEvent $event): void
{
$event->getContext()->setAttribute(
'isAdmin',
(bool)$this->context->getPropertyFromAspect('backend.user', 'isAdmin', false)
);
}
}
Vendor\Users\Domain\Model\User:
properties:
email:
exclude_if: "!context.hasAttribute('isAdmin') || !context.getAttribute('isAdmin')"
Note
If serialized data depends on the current user and the endpoint uses response cache, make sure such responses are not shared between users (see Conditions).
Here you can find out how to customize and extend expression language for serialization