Type converters
Type converters are commonly used when it is necessary to convert from one type
into another. They are usually applied in the Extbase controller in the
initialize<action
method.
For example a date might be given as string in some language,
"October 7th, 2022"
or as UNIX time stamp: 1665159559
.
Your action method, however, expects a \Date
object. Extbase tries to
match the data coming from the frontend automatically.
When matching the data formats is expected to fail you can use one of the type
converters provided by Extbase or implement a type converter yourself
by implementing the interface
\TYPO3\
.
You can find the type converters provided by Extbase in the directory EXT:extbase/Classes/Property/TypeConverter.
Custom type converters
Changed in version 12.0
A type converter has to be registered in your extension's
Configuration/
file. The previous registration method
via \TYPO3\
is not supported anymore.
A custom type converter must implement the interface
\TYPO3\
. In most use cases
it will extend the abstract class
\TYPO3\
, which
already implements this interface.
All type converters should have no internal state, such that they can be used as singletons and multiple times in succession.
The registration and configuration of a type converter is done in the extension's
Services.
:
services:
MyVendor\MyExtension\Property\TypeConverter\MyCustomDateTimeConverter:
tags:
- name: extbase.type_converter
priority: 10
target: \DateTime
sources: int,string
Note
For conversions of Extbase controller action parameters into Extbase domain
model objects the incoming data is usually a numeric type, but in case of an update
action it might as well be an array containing its ID as property __
.
Thus the configuration should list array
as one of its sources:
services:
MyVendor\MyExtension\Property\TypeConverter\MyCustomModelObjectConverter:
tags:
- name: extbase.type_converter
priority: 10
target: MyVendor\MyExtension\Domain\Model\MyCustomModel
sources: int,string,array