From e09082f786643b03a0339a4bde2188124c1832f1 Mon Sep 17 00:00:00 2001
From: Benni Mack <benni@typo3.org>
Date: Mon, 30 Mar 2020 22:31:10 +0200
Subject: [PATCH] [BUGFIX] Render custom preview before fluid preview in Page
 Module

The code that executes the drawItem hook must be called before
the fluid-preview is rendered, in order to allow rendering of custom variables
within the Fluid preview.

This way, it behaves the same way as in the non-Fluidbased page module.

Resolves: #90906
Releases: master
Change-Id: Id7998446b211763da3d3b7be89a4d7016f2fb54f
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/64020
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Achim Fritz <af@achimfritz.de>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Achim Fritz <af@achimfritz.de>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
---
 .../StandardContentPreviewRenderer.php        | 49 ++++++++-----------
 1 file changed, 21 insertions(+), 28 deletions(-)

diff --git a/typo3/sysext/backend/Classes/Preview/StandardContentPreviewRenderer.php b/typo3/sysext/backend/Classes/Preview/StandardContentPreviewRenderer.php
index 35517ff481b0..6293cb8028a4 100644
--- a/typo3/sysext/backend/Classes/Preview/StandardContentPreviewRenderer.php
+++ b/typo3/sysext/backend/Classes/Preview/StandardContentPreviewRenderer.php
@@ -82,6 +82,24 @@ class StandardContentPreviewRenderer implements PreviewRendererInterface, Logger
         $contentTypeLabels = $item->getBackendLayout()->getDrawingConfiguration()->getContentTypeLabels();
         $languageService = $this->getLanguageService();
 
+        $drawItem = true;
+        $hookPreviewContent = '';
+        // Hook: Render an own preview of a record
+        if (!empty($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem'])) {
+            $pageLayoutView = PageLayoutView::createFromDrawingConfiguration($item->getBackendLayout()->getDrawingConfiguration());
+            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem'] ?? [] as $className) {
+                $hookObject = GeneralUtility::makeInstance($className);
+                if (!$hookObject instanceof PageLayoutViewDrawItemHookInterface) {
+                    throw new \UnexpectedValueException($className . ' must implement interface ' . PageLayoutViewDrawItemHookInterface::class, 1582574553);
+                }
+                $hookObject->preProcess($pageLayoutView, $drawItem, $previewHeader, $hookPreviewContent, $record);
+            }
+            $item->setRecord($record);
+        }
+
+        if (!$drawItem) {
+            return $hookPreviewContent;
+        }
         // Check if a Fluid-based preview template was defined for this CType
         // and render it via Fluid. Possible option:
         // mod.web_layout.tt_content.preview.media = EXT:site_mysite/Resources/Private/Templates/Preview/Media.html
@@ -244,36 +262,11 @@ class StandardContentPreviewRenderer implements PreviewRendererInterface, Logger
 
     public function wrapPageModulePreview(string $previewHeader, string $previewContent, GridColumnItem $item): string
     {
-        $drawItem = true;
-        $record = $item->getRecord();
-        $hookPreviewContent = '';
-
-        // Hook: Render an own preview of a record
-        if (!empty($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem'])) {
-            $pageLayoutView = PageLayoutView::createFromDrawingConfiguration($item->getBackendLayout()->getDrawingConfiguration());
-            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem'] ?? [] as $className) {
-                $hookObject = GeneralUtility::makeInstance($className);
-                if (!$hookObject instanceof PageLayoutViewDrawItemHookInterface) {
-                    throw new \UnexpectedValueException($className . ' must implement interface ' . PageLayoutViewDrawItemHookInterface::class, 1582574553);
-                }
-                $hookObject->preProcess($pageLayoutView, $drawItem, $previewHeader, $hookPreviewContent, $record);
-            }
-            $item->setRecord($record);
-        }
-
-        $content = $previewHeader;
-
-        $content .= $hookPreviewContent;
-        if ($drawItem) {
-            $content .= $previewContent;
-        }
-
-        $out = '<span class="exampleContent">' . $content . '</span>';
-
+        $content = '<span class="exampleContent">' . $previewHeader . $previewContent . '</span>';
         if ($item->isDisabled()) {
-            return '<span class="text-muted">' . $out . '</span>';
+            return '<span class="text-muted">' . $content . '</span>';
         }
-        return $out;
+        return $content;
     }
 
     protected function getProcessedValue(GridColumnItem $item, string $fieldList, array &$info): void
-- 
GitLab