Skip to content

Commit 57b910d

Browse files
[FEATURE] Add PSR-14 for record retrieval in DatabaseRecordLinkBuilder (#6277)
* [FEATURE] Add PSR-14 for record retrieval in DatabaseRecordLinkBuilder Resolves: TYPO3-Documentation/Changelog-To-Doc#1405 Releases: main * [TASK] Incorporate review Releases: main
1 parent e227780 commit 57b910d

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
.. include:: /Includes.rst.txt
2+
.. index:: Events; BeforeDatabaseRecordLinkResolvedEvent
3+
.. _BeforeDatabaseRecordLinkResolvedEvent:
4+
5+
=====================================
6+
BeforeDatabaseRecordLinkResolvedEvent
7+
=====================================
8+
9+
.. versionadded:: 14.0
10+
The event :php-short:`TYPO3\CMS\Frontend\Event\BeforeDatabaseRecordLinkResolvedEvent`
11+
has been introduced to retrieve records via custom code in
12+
:php:`TYPO3\CMS\Frontend\Typolink\DatabaseRecordLinkBuilder`.
13+
14+
The PSR-14 event :php:`TYPO3\CMS\Frontend\Event\BeforeDatabaseRecordLinkResolvedEvent`
15+
is dispatched immediately before database record lookup is done
16+
for a link by the DatabaseRecordLinkBuilder and therefore allows
17+
custom functionality to be attached to record retrieval. The event is stoppable,
18+
which means that as soon as a listener returns a record, no further listener
19+
gets called and the core does no further lookup.
20+
21+
The event is dispatched with :php:`$record` set to :php:`null`. If an event
22+
listener retrieves a record from the database, it should set the :php:`$record`
23+
property to the record as an array. This will stop the event propagation and
24+
cause the default record retrieval logic in
25+
:php:`TYPO3\CMS\Frontend\Typolink\DatabaseRecordLinkBuilder` to be skipped.
26+
27+
.. important::
28+
29+
The event is stoppable: Setting the :php:`$record` property to a non-null
30+
value stops event propagation and skips the default record retrieval logic.
31+
32+
Note that the custom code needs to take care - if relevant - of all aspects
33+
normally handled by :php:`TYPO3\CMS\Frontend\Typolink\DatabaseRecordLinkBuilder`,
34+
such as record visibility, language overlay or version overlay.
35+
36+
.. _BeforeDatabaseRecordLinkResolvedEvent-example:
37+
38+
Example
39+
=======
40+
41+
.. literalinclude:: _BeforeDatabaseRecordLinkResolvedEvent/_MyEventListener.php
42+
:language: php
43+
:caption: EXT:my_extension/Classes/Frontend/EventListener/MyEventListener.php
44+
45+
.. _BeforeDatabaseRecordLinkResolvedEvent-api:
46+
47+
API
48+
===
49+
50+
.. include:: /CodeSnippets/Events/Frontend/BeforeDatabaseRecordLinkResolvedEvent.rst.txt
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MyVendor\MyExtension\Frontend\EventListener;
6+
7+
use TYPO3\CMS\Core\Attribute\AsEventListener;
8+
use TYPO3\CMS\Frontend\Event\BeforeDatabaseRecordLinkResolvedEvent;
9+
10+
final readonly class MyEventListener
11+
{
12+
#[AsEventListener(
13+
identifier: 'my-extension/before-database-record-link-resolved',
14+
)]
15+
public function __invoke(BeforeDatabaseRecordLinkResolvedEvent $event): void
16+
{
17+
// Retrieve the record from the database as an array (just an example -
18+
// replace the code in the first line below with your code)
19+
$result = getADatabaseRecord();
20+
if ($result !== false) {
21+
// Setting the record stops event propagation and
22+
// skips the default record retrieval logic
23+
$event->record = $result;
24+
}
25+
}
26+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
.. Generated by https://github.com/TYPO3-Documentation/t3docs-codesnippets
2+
.. php:namespace:: TYPO3\CMS\Frontend\Event
3+
4+
.. php:class:: BeforeDatabaseRecordLinkResolvedEvent
5+
6+
A PSR-14 event fired in the frontend process before database record
7+
lookup is done for a link.
8+
9+
This event makes it possible to implement custom logic, for example,
10+
for specific frontend access when retrieving a linked record in a typolink.
11+
12+
.. php:attr:: linkDetails
13+
:readonly:
14+
:public:
15+
16+
Information about the link being processed
17+
18+
.. php:attr:: databaseTable
19+
:readonly:
20+
:public:
21+
22+
The name of the database the record belongs to
23+
24+
.. php:attr:: typoscriptConfiguration
25+
:readonly:
26+
:public:
27+
28+
The full TypoScript link handler configuration
29+
30+
.. php:attr:: tsConfig
31+
:readonly:
32+
:public:
33+
34+
The full TSconfig link handler configuration
35+
36+
.. php:attr:: request
37+
:readonly:
38+
:public:
39+
40+
The current request object
41+
42+
.. php:attr:: record
43+
:readonly:
44+
:public:
45+
46+
The database record as an array (initially :php:`null`)

0 commit comments

Comments
 (0)