-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backport 13.4] [FEATURE] Enable custom field transformation (#5142)
* [FEATURE] Enable custom field transformation Resolves: TYPO3-Documentation/Changelog-To-Doc#1031 Releases: main, 13.4 * Update Documentation/ApiOverview/DatabaseRecords/RecordObjects.rst --------- Co-authored-by: lina.wolf <[email protected]> Co-authored-by: Lina Wolf <[email protected]>
- Loading branch information
1 parent
fcd5ce7
commit 35aa8dc
Showing
8 changed files
with
198 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
Documentation/ApiOverview/Events/Events/Core/Domain/RecordCreationEvent.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
.. include:: /Includes.rst.txt | ||
.. index:: Events; RecordCreationEvent | ||
.. _RecordCreationEvent: | ||
|
||
=================== | ||
RecordCreationEvent | ||
=================== | ||
|
||
.. versionadded:: 13.3 | ||
The PSR-14 :php-short:`\TYPO3\CMS\Core\Domain\Event\RecordCreationEvent` is introduced in | ||
order to allow the manipulation of any property | ||
before being used to create a | ||
`Database Record <https://docs.typo3.org/permalink/t3coreapi:database-records>`_ object. | ||
|
||
The `Database Record <https://docs.typo3.org/permalink/t3coreapi:database-records>`_ 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 | ||
:php-short:`\TYPO3\CMS\Core\Domain\RecordInterface`, | ||
:php-short:`\TYPO3\CMS\Extbase\Domain\Model\FileReference`, | ||
:php:`\TYPO3\CMS\Core\Resource\Folder` or :php:`\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 <https://docs.typo3.org/permalink/t3tca:columns-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 :php-short:`\Psr\EventDispatcher\StoppableEventInterface`), which | ||
allows listeners to actually create a Record object, implementing | ||
:php:`\TYPO3\CMS\Core\Domain\RecordInterface` completely on their | ||
own. | ||
|
||
.. _RecordCreationEvent-example: | ||
|
||
Example | ||
======= | ||
|
||
The event listener class, using the PHP attribute :php:`#[AsEventListener]` for | ||
registration, creates a :php:`Coordinates` object based on the field value of | ||
the :php:`coordinates` field for the custom :php:`maps` content type. | ||
|
||
.. literalinclude:: _RecordCreationEvent/_MyEventListener.php | ||
:caption: EXT:my_extension/Classes/Domain/EventListener/MyEventListener.php | ||
|
||
.. include:: /_includes/EventsAttributeAdded.rst.txt | ||
|
||
The model could for example look like this: | ||
|
||
.. literalinclude:: _RecordCreationEvent/_Coordinates.php | ||
:caption: EXT:my_extension/Classes/Domain/Model/Coordinates.php | ||
|
||
.. _RecordCreationEvent-api: | ||
|
||
API | ||
=== | ||
|
||
.. include:: /CodeSnippets/Events/Core/RecordCreationEvent.rst.txt | ||
|
||
.. important:: | ||
|
||
The event operates on the :php-short:`\TYPO3\CMS\Core\Domain\RecordInterface` | ||
instead of an actual implementation. This way, extension authors are able | ||
to set custom records, implementing the interface. |
25 changes: 25 additions & 0 deletions
25
Documentation/ApiOverview/Events/Events/Core/Domain/_RecordCreationEvent/_Coordinates.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?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]); | ||
} | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...mentation/ApiOverview/Events/Events/Core/Domain/_RecordCreationEvent/_MyEventListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?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')), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
Documentation/CodeSnippets/Events/Core/RecordCreationEvent.rst.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
.. Generated by https://github.com/TYPO3-Documentation/t3docs-codesnippets | ||
.. php:namespace:: TYPO3\CMS\Core\Domain\Event | ||
.. php:class:: 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. | ||
|
||
.. php:method:: setRecord(\TYPO3\CMS\Core\Domain\RecordInterface $record) | ||
:param $record: the record | ||
|
||
.. php:method:: isPropagationStopped() | ||
:returns: `bool` | ||
|
||
.. php:method:: hasProperty(string $name) | ||
:returns: `bool` | ||
|
||
:param $name: the name | ||
|
||
.. php:method:: setProperty(string $name, ?mixed $propertyValue) | ||
:param $name: the name | ||
:param $propertyValue: the propertyValue | ||
|
||
.. php:method:: setProperties(array $properties) | ||
:param $properties: the properties | ||
|
||
.. php:method:: unsetProperty(string $name) | ||
:returns: `bool` | ||
|
||
:param $name: the name | ||
|
||
.. php:method:: getProperty(string $name) | ||
:returns: `?mixed` | ||
|
||
:param $name: the name | ||
|
||
.. php:method:: getProperties() | ||
:returns: `array` | ||
|
||
.. php:method:: getRawRecord() | ||
:returns: `\TYPO3\CMS\Core\Domain\RawRecord` | ||
|
||
.. php:method:: getSystemProperties() | ||
:returns: `\TYPO3\CMS\Core\Domain\Record\SystemProperties` | ||
|
||
.. php:method:: getContext() | ||
:returns: `\TYPO3\CMS\Core\Context\Context` |