Handlers 

Serializer handlers take over (de)serialization of a value of given type. T3api delivers handlers for things which are common in TYPO3 but which plain JSON serialization can not handle well - files, processed images, links to records and typolinks.

Some handlers are used automatically (based on the class of the value), others are activated by setting the type of a property or virtual property - with annotation from \SourceBroker\T3api\Annotation\Serializer\Type namespace or with type key in YAML metadata.

Handler Activated by Output
FileReferenceHandler automatically for classes extending Extbase's FileReference or Folder nested in an entity (on deserialization also File and its subclasses) object with url, uid and file data
ImageHandler @T3api\Serializer\Type\Image, type Image<...> absolute URL of processed image
RecordUriHandler @T3api\Serializer\Type\RecordUri, type RecordUri<...> absolute URL of record (e.g. news detail page)
TypolinkHandler @T3api\Serializer\Type\Typolink, type Typolink absolute URL built from typolink value
RteHandler @T3api\Serializer\Type\Rte, type Rte HTML processed by lib.parseFunc_RTE
PasswordHashHandler @T3api\Serializer\Type\PasswordHash, type PasswordHash on deserialization: hashed password; on serialization: null (stored hash with serialize=true)
CurrentFeUserHandler @T3api\Serializer\Type\CurrentFeUser see Current user assignment

FileReference 

Objects of classes extending Extbase's \TYPO3\CMS\Extbase\Domain\Model\FileReference or Folder (e.g. \GeorgRinger\News\Domain\Model\FileReference or your own file reference class) nested in an entity are serialized automatically, there is nothing to configure. Objects of classes extending File are serialized like other objects. Example of falMedia property of news:

"falMedia": [
    {
        "url": "https://14.t3api.ddev.site/fileadmin/user_upload/test1.jpg",
        "uid": 4,
        "file": {
            "uid": 1,
            "name": "test1.jpg",
            "mimeType": "image/jpeg",
            "size": 42520
        }
    }
]
Copied!
  • url - absolute URL of the original file.
  • uid - uid of the file reference (sys_file_reference).
  • file - data of the original file (sys_file). Contains fileToken when the token is returned for the file, see File token.
  • urlEmbed - only for videos (e.g. YouTube, Vimeo) - URL which can be used in <iframe>.

The structure of the output is fixed - other properties of the file reference (title, alternative, link...) are not added to it, even if they are configured in serialization metadata. If you need them in the response, expose them with a virtual property of the parent entity or with a custom handler.

The same handler is used for deserialization, also of properties of type File or a class extending it - see Handling File Upload how to attach files in POST / PATCH payload.

Image 

Returns absolute URL of an image processed by TYPO3 (resized / cropped) - so frontend does not need to load original, possibly huge, files. It can be used on a property or a method returning FileReference, uid of sys_file_reference, or a list (e.g. ObjectStorage) of them - then a list of URLs is returned.

Parameters (all optional; values are the same as in f:image ViewHelper, so c and m suffixes work):

  • width
  • height
  • maxWidth
  • maxHeight
  • cropVariant - name of the crop variant (default default).
use SourceBroker\T3api\Annotation as T3api;

class News extends \GeorgRinger\News\Domain\Model\News
{
    /**
     * @T3api\Serializer\VirtualProperty("imageThumbnail")
     * @T3api\Serializer\Type\Image(width="380", height="250c")
     */
    public function getImageThumbnail(): ?FileReference
    {
        return $this->getFalMedia()->count() ? $this->getFalMedia()->toArray()[0] : null;
    }
}
Copied!

The same in YAML metadata (parameters are positional: width, height, maxWidth, maxHeight, cropVariant). This is how imageThumbnail and imageLarge are configured in EXT:t3apinews:

SourceBroker\T3apinews\Domain\Model\News:
  virtual_properties:
    getImageThumbnail:
      serialized_name: imageThumbnail
      groups: [api_get_collection_t3apinews_news, api_get_item_t3apinews_news]
      type: "Image<'380','250c','','',''>"
    getImageLarge:
      serialized_name: imageLarge
      groups: [api_get_collection_t3apinews_news, api_get_item_t3apinews_news]
      type: "Image<'1280','768','','',''>"
Copied!

RecordUri 

Returns absolute URL to the record, built with TYPO3 record link handler (t3://record?identifier=...&uid=...). The only parameter is the identifier of the link handler configured in config.recordLinks TypoScript. The value of the property itself is not used - uid of the serialized entity is taken, so it is usually used on a virtual property.

config.recordLinks.tx_news {
    typolink {
        parameter = {$plugin.tx_t3apinews.pageUid.newsDetails}
        additionalParams.data = field:uid
        additionalParams.wrap = &tx_news_pi1[controller]=News&tx_news_pi1[action]=detail&tx_news_pi1[news]=|
    }
}
Copied!
/**
 * @T3api\Serializer\VirtualProperty("singleUri")
 * @T3api\Serializer\Type\RecordUri("tx_news")
 */
public function getSingleUri(): string
{
    return '';
}
Copied!
SourceBroker\T3apinews\Domain\Model\News:
  virtual_properties:
    getSingleUri:
      serialized_name: singleUri
      groups: [api_get_collection_t3apinews_news, api_get_item_t3apinews_news]
      type: "RecordUri<'tx_news'>"
Copied!

If the URL can not be resolved an empty string is returned.

Rte 

Processes the value with lib.parseFunc_RTE (as f:format.html does), so t3:// links inside RTE content are converted into real URLs.

/**
 * @T3api\Serializer\Type\Rte
 */
protected string $bodytext = '';
Copied!

PasswordHash 

Used in deserialization only - plain password sent in the payload is hashed with default frontend hashing method before it is set on the entity. Useful for registration endpoints.

/**
 * @T3api\Serializer\Type\PasswordHash
 */
protected $password = '';
Copied!

On serialization null is returned, so the stored password hash is not included in API responses. If you need to read the hash (e.g. to migrate users to another system), enable it explicitly with serialize=true:

/**
 * @T3api\Serializer\Type\PasswordHash(serialize=true)
 */
protected $password = '';
Copied!

or in YAML metadata:

Vendor\Extension\Domain\Model\User:
  properties:
    password:
      type: "PasswordHash<'serialize'>"
Copied!

Custom handlers 

Custom handler is a class implementing JMS \JMS\Serializer\Handler\SubscribingHandlerInterface. The easiest way is to extend \SourceBroker\T3api\Serializer\Handler\AbstractHandler and implement SerializeHandlerInterface and/or DeserializeHandlerInterface - then only the list of supported types is needed.

<?php

namespace Vendor\Extension\Serializer\Handler;

use JMS\Serializer\SerializationContext;
use JMS\Serializer\Visitor\SerializationVisitorInterface;
use SourceBroker\T3api\Serializer\Handler\AbstractHandler;
use SourceBroker\T3api\Serializer\Handler\SerializeHandlerInterface;

class PriceHandler extends AbstractHandler implements SerializeHandlerInterface
{
    public const TYPE = 'Price';

    protected static $supportedTypes = [self::TYPE];

    public function serialize(
        SerializationVisitorInterface $visitor,
        $value,
        array $type,
        SerializationContext $context
    ): string {
        $currency = $type['params'][0] ?? 'EUR';

        return number_format((int)$value / 100, 2, '.', '') . ' ' . $currency;
    }
}
Copied!

Register the handler in ext_localconf.php. Handlers are created with GeneralUtility::makeInstance(), so constructor dependency injection works if the class is public in Services.yaml.

$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3api']['serializerHandlers'][]
    = \Vendor\Extension\Serializer\Handler\PriceHandler::class;
Copied!

And use the type in YAML metadata:

Vendor\Extension\Domain\Model\Product:
  properties:
    priceInCents:
      serialized_name: price
      type: "Price<'USD'>"
Copied!

To use the type as annotation, create a class implementing \SourceBroker\T3api\Annotation\Serializer\Type\TypeInterface (see \SourceBroker\T3api\Annotation\Serializer\Type\Image as an example) - getName() returns the type name and getParams() the list of parameters.