Feature: #104846 - Custom field transformations for new records
See forge#104846
Description
With forge#103783 the new
\TYPO3\ object has been introduced, which
is an object representing a raw database record based on TCA and is usually
used in the frontend (via Fluid Templates).
Since Feature: #103581 - Automatically transform TCA field values for record objects the properties of those
Record
objects are transformed / expanded from their raw database value into
"rich-flavored" values. Those values might be relations to e.g.
Record ,
File,
Folder or
\Date objects.
However, TYPO3 does not know about custom field meanings, e.g. latitude and
longitude information, stored in an input field or user settings stored as
JSON in an TCA type json field. For such custom needs, the new
PSR-14
\TYPO3\ has been
introduced. It is dispatched right before a
Record is created and
therefore allows to fully manipulate any property, even the ones already
transformed by TYPO3.
The new event is stoppable (implementing
\Stoppable), which
allows listeners to actually create a
Record object completely on their
own.
Important
The event operates on the
Record instead of an actual
implementation. This way, extension authors are able to set custom records,
implementing the interface.
The new event features the following methods:
set- Manually adds aRecord () Recordobject (stops the event propagation)Interface has- Whether a property existsProperty () set- Add or overwrite a propertyProperty () set- Set properties for theProperties () RecordInterface unset- Unset a single propertyProperty () get- Get the value for a single propertyProperty () get- Get all propertiesProperties () get- Get theRaw Record () RawobjectRecord get- Get the calculatedSystem Properties () SystemProperties get- Get the currentContext () Context(used to fetch the raw database row)is- Whether the event propagation is stoppedPropagation Stopped ()
Example
The event listener class, using the PHP attribute
# for
registration, creates a
Coordinates object based on the field value of
the
coordinates field for the custom
maps content type.
final class RecordCreationEventListener
{
#[AsEventListener]
public function __invoke(\TYPO3\CMS\Core\Domain\Event\RecordCreationEvent $event): void
{
$rawRecord = $event->getRawRecord();
if ($rawRecord->getMainType() === 'tt_content' && $rawRecord->getRecordType() === 'maps' && $event->hasProperty('coordinates')) {
$event->setProperty(
'coordinates',
new Coordinates($event->getProperty('coordinates'))
);
}
}
}
Impact
Using the new PSR-14
Record,
extension authors are able to apply any field transformation to any property before a
Record is created.
It is even possible to completely create a new
Record object on their own.