RecordCreationEvent
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
Record,
File,
\TYPO3\ or
\Date 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
\Stoppable), which
allows listeners to actually create a Record object, implementing
\TYPO3\ completely on their
own.
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.
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Domain\EventListener;
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')),
);
}
}
The model could for example look like this:
<?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]);
}
}
}
}
API
- class RecordCreationEvent
-
- Fully qualified name
-
\TYPO3\
CMS\ Core\ Domain\ Event\ Record Creation Event
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.
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.