Attention
TYPO3 v11 has reached end-of-life as of October 31th 2024 and is no longer being maintained. Use the version switcher on the top left of this page to select documentation for a supported version of TYPO3.
Need more time before upgrading? You can purchase Extended Long Term Support (ELTS) for TYPO3 v11 here: TYPO3 ELTS.
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
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
ext_
:
<?php
declare(strict_types=1);
use MyVendor\MyExtension\TypeConverter\MyDatetimeConverter;
use TYPO3\CMS\Extbase\Utility\ExtensionUtility;
defined('TYPO3') or die();
// Starting with TYPO3 v12 extbase type converters are registered in
// Configuration/Services.yaml
if ((new \TYPO3\CMS\Core\Information\Typo3Version())->getMajorVersion() < 12)
{
// Register type converters
ExtensionUtility::registerTypeConverter(MyDatetimeConverter::class);
}
Tip
Starting with TYPO3 v12.0 a type converter is registered in the extension's
Services.
. See
Type converters in TYPO3 v12.
To provide compatibility with both TYPO3 v11 and v12 register the type
converter in the Services.
and keep the call to
Extension
until dropping v11 support.