From 94d3ed942914eb23fa5c4295577c5e71739fa38e Mon Sep 17 00:00:00 2001 From: Achim Fritz <achim.fritz@b13.de> Date: Wed, 9 Nov 2022 09:44:28 +0100 Subject: [PATCH] [BUGFIX] Allow changing record data in event The new event PageContentPreviewRenderingEvent introduced in TYPO3 v12 now allows to modify the record itself, making it as flexible as the previous hook. Resolves: #99026 Releases: main Change-Id: I475c797c88e765972a915f77aeb0f0441d7548d1 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/76514 Tested-by: Benni Mack <benni@typo3.org> Tested-by: core-ci <typo3@b13.com> Tested-by: Oliver Bartsch <bo@cedev.de> Reviewed-by: Benni Mack <benni@typo3.org> Reviewed-by: Oliver Bartsch <bo@cedev.de> --- .../View/BackendLayout/Grid/GridColumnItem.php | 15 +++++++++++---- .../Event/PageContentPreviewRenderingEvent.php | 9 +++++++-- .../Feature-98375-PSR-14EventsInPageModule.rst | 4 ++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/typo3/sysext/backend/Classes/View/BackendLayout/Grid/GridColumnItem.php b/typo3/sysext/backend/Classes/View/BackendLayout/Grid/GridColumnItem.php index d52b474caf4e..44e12cbde6df 100644 --- a/typo3/sysext/backend/Classes/View/BackendLayout/Grid/GridColumnItem.php +++ b/typo3/sysext/backend/Classes/View/BackendLayout/Grid/GridColumnItem.php @@ -72,18 +72,25 @@ class GridColumnItem extends AbstractGridObject public function getPreview(): string { - $record = $this->getRecord(); $previewRenderer = GeneralUtility::makeInstance(StandardPreviewRendererResolver::class) ->resolveRendererFor( 'tt_content', - $record, + $this->record, $this->context->getPageId() ); $previewHeader = $previewRenderer->renderPageModulePreviewHeader($this); - $event = new PageContentPreviewRenderingEvent('tt_content', $this->record, $this->context); - GeneralUtility::makeInstance(EventDispatcherInterface::class)->dispatch($event); + // Dispatch event to allow listeners adding an alternative content type + // specific preview or to manipulate the content elements' record data. + $event = GeneralUtility::makeInstance(EventDispatcherInterface::class)->dispatch( + new PageContentPreviewRenderingEvent('tt_content', $this->record, $this->context) + ); + + // Update the modified record data + $this->record = $event->getRecord(); + // Get specific preview from listeners. In case non was added, + // fall back to the standard preview rendering workflow. $previewContent = $event->getPreviewContent(); if ($previewContent === null) { $previewContent = $previewRenderer->renderPageModulePreviewContent($this); diff --git a/typo3/sysext/backend/Classes/View/Event/PageContentPreviewRenderingEvent.php b/typo3/sysext/backend/Classes/View/Event/PageContentPreviewRenderingEvent.php index 2588f266219e..705a786a613d 100644 --- a/typo3/sysext/backend/Classes/View/Event/PageContentPreviewRenderingEvent.php +++ b/typo3/sysext/backend/Classes/View/Event/PageContentPreviewRenderingEvent.php @@ -28,9 +28,9 @@ final class PageContentPreviewRenderingEvent implements StoppableEventInterface private ?string $content = null; public function __construct( - private string $table, + private readonly string $table, private array $record, - private PageLayoutContext $context + private readonly PageLayoutContext $context ) { } @@ -44,6 +44,11 @@ final class PageContentPreviewRenderingEvent implements StoppableEventInterface return $this->record; } + public function setRecord(array $record): void + { + $this->record = $record; + } + public function getPageLayoutContext(): PageLayoutContext { return $this->context; diff --git a/typo3/sysext/core/Documentation/Changelog/12.0/Feature-98375-PSR-14EventsInPageModule.rst b/typo3/sysext/core/Documentation/Changelog/12.0/Feature-98375-PSR-14EventsInPageModule.rst index 597b61eaddfc..796e087ed7be 100644 --- a/typo3/sysext/core/Documentation/Changelog/12.0/Feature-98375-PSR-14EventsInPageModule.rst +++ b/typo3/sysext/core/Documentation/Changelog/12.0/Feature-98375-PSR-14EventsInPageModule.rst @@ -33,7 +33,7 @@ in a column that isn't on a Backend Layout. Use :php:`ModifyDatabaseQueryForContentEvent` to filter out certain content elements from being shown in the Page Module. -Use :php:`PageContentPreviewRenderingEvent` to ship an alternative rendering for a -specific content type. +Use :php:`PageContentPreviewRenderingEvent` to ship an alternative rendering for +a specific content type or to manipulate the content elements' record data. .. index:: Backend, PHP-API, ext:backend -- GitLab