Feature: #104846 - Custom field transformations for new records 

See forge#104846

Description 

With forge#103783 the new \TYPO3\CMS\Core\Domain\Record 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 , FileReference , Folder or \DateTimeImmutable 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\CMS\Core\Domain\Event\RecordCreationEvent 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 \StoppableEventInterface ), which allows listeners to actually create a Record object completely on their own.

The new event features the following methods:

  • setRecord() - Manually adds a RecordInterface object (stops the event propagation)
  • hasProperty() - Whether a property exists
  • setProperty() - Add or overwrite a property
  • setProperties() - Set properties for the RecordInterface
  • unsetProperty() - Unset a single property
  • getProperty() - Get the value for a single property
  • getProperties() - Get all properties
  • getRawRecord() - Get the RawRecord object
  • getSystemProperties() - Get the calculated SystemProperties
  • getContext() - Get the current Context (used to fetch the raw database row)
  • isPropagationStopped() - Whether the event propagation is stopped

Example 

The event listener class, using the PHP attribute #[AsEventListener] 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'))
            );
        }
    }
}
Copied!

Impact 

Using the new PSR-14 RecordCreationEvent , 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 RecordInterface object on their own.