-
Notifications
You must be signed in to change notification settings - Fork 443
[FEATURE] Add PSR-14 for record retrieval in DatabaseRecordLinkBuilder #6277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sarahmccarthy123
wants to merge
1
commit into
main
Choose a base branch
from
feature/event-DatabaseRecordLinkBuilder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+115
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
...on/ApiOverview/Events/Events/Frontend/BeforeDatabaseRecordLinkResolvedEvent.rst
This file contains hidden or 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,59 @@ | ||
| .. include:: /Includes.rst.txt | ||
| .. index:: Events; BeforeDatabaseRecordLinkResolvedEvent | ||
| .. _BeforeDatabaseRecordLinkResolvedEvent: | ||
|
|
||
| ===================================== | ||
| BeforeDatabaseRecordLinkResolvedEvent | ||
| ===================================== | ||
|
|
||
| .. versionadded:: 14.0 | ||
| The event :php-short:`TYPO3\CMS\Frontend\Event\BeforeDatabaseRecordLinkResolvedEvent` | ||
| has been introduced to retrieve records via custom code in | ||
| :php:`TYPO3\CMS\Frontend\Typolink\DatabaseRecordLinkBuilder`. | ||
|
|
||
| The PSR-14 event :php:`TYPO3\CMS\Frontend\Event\BeforeDatabaseRecordLinkResolvedEvent` | ||
| is dispatched immediately before database record lookup is done | ||
| for a link by the DatabaseRecordLinkBuilder and therefore allows | ||
| custom functionality to be attached to record retrieval. The event is stoppable, | ||
| which means that as soon as a listener returns a record, no further listener | ||
| gets called and the core does no further lookup. | ||
|
|
||
| The event is dispatched with :php:`$record` set to :php:`null`. If an event | ||
| listener retrieves a record from the database, it should set the :php:`$record` | ||
| property to the record as an array. This will stop the event propagation and | ||
| cause the default record retrieval logic in | ||
| :php:`TYPO3\CMS\Frontend\Typolink\DatabaseRecordLinkBuilder` to be skipped. | ||
|
|
||
| .. important:: | ||
|
|
||
| The event is stoppable: Setting the :php:`$record` property to a non-null | ||
| value stops event propagation and skips the default record retrieval logic. | ||
|
|
||
| Note that the custom code needs to take care - if relevant - of all aspects | ||
| normally handled by :php:`TYPO3\CMS\Frontend\Typolink\DatabaseRecordLinkBuilder`, | ||
| such as record visibility, language overlay or version overlay. | ||
|
|
||
| The event provides access to the following public properties: | ||
|
|
||
| * :php:`$linkDetails`: Information about the link being processed | ||
| * :php:`$databaseTable`: The name of the database the record belongs to | ||
| * :php:`$typoscriptConfiguration`: The full TypoScript link handler configuration | ||
| * :php:`$tsConfig`: The full TSconfig link handler configuration | ||
| * :php:`$request`: The current request object | ||
| * :php:`$record`: The database record as an array (initially :php:`null`) | ||
|
|
||
| .. _BeforeDatabaseRecordLinkResolvedEvent-example: | ||
|
|
||
| Example | ||
| ======= | ||
|
|
||
| .. literalinclude:: _BeforeDatabaseRecordLinkResolvedEvent/_MyEventListener.php | ||
| :language: php | ||
| :caption: EXT:my_extension/Classes/Frontend/EventListener/MyEventListener.php | ||
|
|
||
| .. _BeforeDatabaseRecordLinkResolvedEvent-api: | ||
|
|
||
| API | ||
| === | ||
|
|
||
| .. include:: /CodeSnippets/Events/Frontend/BeforeDatabaseRecordLinkResolvedEvent.rst.txt | ||
26 changes: 26 additions & 0 deletions
26
...erview/Events/Events/Frontend/_BeforeDatabaseRecordLinkResolvedEvent/_MyEventListener.php
This file contains hidden or 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,26 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace MyVendor\MyExtension\Frontend\EventListener; | ||
|
|
||
| use TYPO3\CMS\Core\Attribute\AsEventListener; | ||
| use TYPO3\CMS\Frontend\Event\BeforeDatabaseRecordLinkResolvedEvent; | ||
|
|
||
| final readonly class MyEventListener | ||
| { | ||
| #[AsEventListener( | ||
| identifier: 'my-extension/before-database-record-link-resolved', | ||
| )] | ||
| public function __invoke(BeforeDatabaseRecordLinkResolvedEvent $event): void | ||
| { | ||
| // Retrieve the record from the database as an array (just an example - | ||
| // replace the code in the first line below with your code) | ||
| $result = getADatabaseRecord(); | ||
| if ($result !== false) { | ||
| // Setting the record stops event propagation and | ||
| // skips the default record retrieval logic | ||
| $event->record = $result; | ||
| } | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
...tion/CodeSnippets/Events/Frontend/BeforeDatabaseRecordLinkResolvedEvent.rst.txt
This file contains hidden or 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,30 @@ | ||
| .. Generated by https://github.com/TYPO3-Documentation/t3docs-codesnippets | ||
| .. php:namespace:: TYPO3\CMS\Frontend\Event | ||
|
|
||
| .. php:class:: BeforeDatabaseRecordLinkResolvedEvent | ||
|
|
||
| A PSR-14 event fired in the frontend process before database record | ||
| lookup is done for a link. | ||
|
|
||
| This event makes it possible to implement custom logic, for example, | ||
| for specific frontend access when retrieving a linked record in a typolink. | ||
|
|
||
| .. php:attr:: linkDetails | ||
| :readonly: | ||
| :public: | ||
|
|
||
| .. php:attr:: databaseTable | ||
| :readonly: | ||
| :public: | ||
|
|
||
| .. php:attr:: typoscriptConfiguration | ||
| :readonly: | ||
| :public: | ||
|
|
||
| .. php:attr:: request | ||
| :readonly: | ||
| :public: | ||
|
|
||
| .. php:attr:: record | ||
| :readonly: | ||
| :public: |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need this here as it is already available in the API below.