RecordCreationEvent

New in version 13.3

The Database Record object, which represents a raw database record based on TCA and is usually used in the frontend (via Fluid Templates).

The properties of those Record objects are transformed / expanded from their raw database value into "rich-flavored" values. Those values might be relations to Record objects implementing RecordInterface , FileReference , \TYPO3\CMS\Core\Resource\Folder or \DateTimeImmutable objects.

TYPO3 does not know about custom field meanings, for example latitude and longitude information, stored in an input field or user settings stored as JSON in a TCA type json field.

This event is dispatched right before a Record object 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, implementing \TYPO3\CMS\Core\Domain\RecordInterface completely on their own.

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.

EXT:my_extension/Classes/Domain/EventListener/MyEventListener.php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\Domain\Access;

use MyVendor\MyExtension\Domain\Model\Coordinates;
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Core\Domain\Event\RecordCreationEvent;

final readonly class MyEventListener
{
    #[AsEventListener]
    public function __invoke(RecordCreationEvent $event): void
    {
        $rawRecord = $event->getRawRecord();
        if ($rawRecord->getMainType() !== 'tt_content') {
            return;
        }
        if ($rawRecord->getRecordType() !== 'maps') {
            return;
        }
        if (!$event->hasProperty('coordinates')) {
            return;
        }
        $event->setProperty(
            'coordinates',
            new Coordinates($event->getProperty('coordinates')),
        );
    }
}
Copied!

New in version 13.0

The model could for example look like this:

EXT:my_extension/Classes/Domain/Model/Coordinates.php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\Domain\Model;

class Coordinates
{
    public float $latitude = 0.0;
    public float $longitude = 0.0;

    /**
     * @param mixed $value - Accepts a string (e.g., "12.34,56.78")
     */
    public function __construct(mixed $value)
    {
        if (is_string($value)) {
            $parts = explode(',', $value);
            if (count($parts) === 2) {
                $this->latitude = (float)trim($parts[0]);
                $this->longitude = (float)trim($parts[1]);
            }
        }
    }
}
Copied!

API

class RecordCreationEvent
Fully qualified name
\TYPO3\CMS\Core\Domain\Event\RecordCreationEvent

Event which allows to manipulate the properties to be used for a new Record.

With this event, it's even possible to create a new Record manually.

setRecord ( \TYPO3\CMS\Core\Domain\RecordInterface $record)
param $record

the record

isPropagationStopped ( )
Returns
bool
hasProperty ( string $name)
param $name

the name

Returns
bool
setProperty ( string $name, ?mixed $propertyValue)
param $name

the name

param $propertyValue

the propertyValue

setProperties ( array $properties)
param $properties

the properties

unsetProperty ( string $name)
param $name

the name

Returns
bool
getProperty ( string $name)
param $name

the name

Returns
?mixed
getProperties ( )
Returns
array
getRawRecord ( )
Returns
\TYPO3\CMS\Core\Domain\RawRecord
getSystemProperties ( )
Returns
\TYPO3\CMS\Core\Domain\Record\SystemProperties
getContext ( )
Returns
\TYPO3\CMS\Core\Context\Context