From d8f032e2eda189ecf0739c1a3fe68919e895af7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BCrk?= Date: Sat, 14 Dec 2024 21:50:32 +0100 Subject: [PATCH] [BUGFIX] Ender preview translated flag information in TYPO3 v12 TYPO3 v12 removed the `contentPostProc-all` aling with other hooks in favour of new PSR-14 `AfterCacheableContentIsGeneratedEvent`. The `DeeplPreviewFlagGeneratePageHook` is not called in TYPO3 v12 anymore and the translated preview flag is not displayed. Changed behavour between TYPO3 v11 and v12 was not intented and simply an oversight to mitigate when adding TYPO3 v12 support. Sadly, this has not been detected for quite a while now. This change introduces a event listener for the TYPO3 v12 event, rendering the same flag to restore the missing and wanted preview flag while keeping the hook implementation for TYPO3 v11 instances. Choosen strategy follows the recommended way to migrate from the hook to the event when dual TYPO3 version support is required. Used command(s): ```base Build/Scripts/runTests.sh -t 11 -p 7.4 -s composerUpdate Build/Scripts/runTests.sh -t 11 -p 7.4 -s phpstanGenerateBaseline Build/Scripts/runTests.sh -t 11 -p 8.1 -s composerUpdate Build/Scripts/runTests.sh -t 12 -p 8.1 -s phpstanGenerateBaseline ``` [1] https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.0/Breaking-97862-HooksRelatedToGeneratingPageContentRemoved.html [2] https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.0/Feature-97862-NewPSR-14EventsForManipulatingFrontendPageGenerationAndCacheBehaviour.html --- Build/phpstan/Core11/phpstan-baseline.neon | 5 -- Build/phpstan/Core11/phpstan.neon | 5 ++ Build/phpstan/Core12/phpstan-baseline.neon | 5 -- Build/phpstan/Core12/phpstan.neon | 4 + ...derTranslatedFlagInFrontendPreviewMode.php | 77 +++++++++++++++++++ Configuration/Services.php | 13 ++++ ext_localconf.php | 7 +- 7 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 Classes/Event/Listener/RenderTranslatedFlagInFrontendPreviewMode.php diff --git a/Build/phpstan/Core11/phpstan-baseline.neon b/Build/phpstan/Core11/phpstan-baseline.neon index b8022556..7c440c4d 100644 --- a/Build/phpstan/Core11/phpstan-baseline.neon +++ b/Build/phpstan/Core11/phpstan-baseline.neon @@ -95,11 +95,6 @@ parameters: count: 1 path: ../../../Classes/Hooks/ButtonBarHook.php - - - message: "#^There is no aspect \"frontend\\.preview\" configured so we can't figure out the exact type to return when calling TYPO3\\\\CMS\\\\Core\\\\Context\\\\Context\\:\\:getPropertyFromAspect$#" - count: 1 - path: ../../../Classes/Hooks/DeeplPreviewFlagGeneratePageHook.php - - message: "#^Parameter \\#1 \\$uid of method WebVision\\\\WvDeepltranslate\\\\Domain\\\\Repository\\\\GlossaryEntryRepository\\:\\:findEntryByUid\\(\\) expects int, int\\|string given\\.$#" count: 1 diff --git a/Build/phpstan/Core11/phpstan.neon b/Build/phpstan/Core11/phpstan.neon index fde01df5..de339190 100644 --- a/Build/phpstan/Core11/phpstan.neon +++ b/Build/phpstan/Core11/phpstan.neon @@ -17,3 +17,8 @@ parameters: - ../../../Tests/Functional/Updates/Fixtures/Extension/test_extension/ext_emconf.php - ../../../Tests/Functional/Fixtures/Extensions/test_services_override/ext_emconf.php - ../../../Classes/Override/Core12/* + - ../../../Classes/Event/Listener/RenderTranslatedFlagInFrontendPreviewMode.php + + typo3: + contextApiGetAspectMapping: + 'frontend.preview': TYPO3\CMS\Frontend\Aspect\PreviewAspect diff --git a/Build/phpstan/Core12/phpstan-baseline.neon b/Build/phpstan/Core12/phpstan-baseline.neon index e7ebfab5..9fcdc7a6 100644 --- a/Build/phpstan/Core12/phpstan-baseline.neon +++ b/Build/phpstan/Core12/phpstan-baseline.neon @@ -80,11 +80,6 @@ parameters: count: 1 path: ../../../Classes/Hooks/ButtonBarHook.php - - - message: "#^There is no aspect \"frontend\\.preview\" configured so we can't figure out the exact type to return when calling TYPO3\\\\CMS\\\\Core\\\\Context\\\\Context\\:\\:getPropertyFromAspect$#" - count: 1 - path: ../../../Classes/Hooks/DeeplPreviewFlagGeneratePageHook.php - - message: "#^PHPDoc tag @throws with type Doctrine\\\\DBAL\\\\DBALException\\|Doctrine\\\\DBAL\\\\Driver\\\\Exception\\|TYPO3\\\\CMS\\\\Core\\\\Exception is not subtype of Throwable$#" count: 1 diff --git a/Build/phpstan/Core12/phpstan.neon b/Build/phpstan/Core12/phpstan.neon index 713f1727..6120439f 100644 --- a/Build/phpstan/Core12/phpstan.neon +++ b/Build/phpstan/Core12/phpstan.neon @@ -17,3 +17,7 @@ parameters: - ../../../Tests/Functional/Updates/Fixtures/Extension/test_extension/ext_emconf.php - ../../../Tests/Functional/Fixtures/Extensions/test_services_override/ext_emconf.php - ../../../Classes/Override/Core11/* + + typo3: + contextApiGetAspectMapping: + 'frontend.preview': TYPO3\CMS\Frontend\Aspect\PreviewAspect diff --git a/Classes/Event/Listener/RenderTranslatedFlagInFrontendPreviewMode.php b/Classes/Event/Listener/RenderTranslatedFlagInFrontendPreviewMode.php new file mode 100644 index 00000000..15879dfb --- /dev/null +++ b/Classes/Event/Listener/RenderTranslatedFlagInFrontendPreviewMode.php @@ -0,0 +1,77 @@ +getTypoScriptFrontendController($event); + $context = $controller->getContext(); + if ( + !$this->isInPreviewMode($context) + || $this->processWorkspacePreview($context) + || ($controller->config['config']['disablePreviewNotification'] ?? false) + || ( + isset($controller->page['tx_wvdeepltranslate_translated_time']) + && $controller->page['tx_wvdeepltranslate_translated_time'] === 0 + ) + ) { + // Preview flag must not be inserted. Return early. + return; + } + + $messagePreviewLabel = ($controller->config['config']['deepl_message_preview'] ?? '') + ?: 'Translated with DeepL'; + + $styles = []; + $styles[] = 'position: fixed'; + $styles[] = 'top: 65px'; + $styles[] = 'right: 15px'; + $styles[] = 'padding: 8px 18px'; + $styles[] = 'background: #006494'; + $styles[] = 'border: 1px solid #006494'; + $styles[] = 'font-family: sans-serif'; + $styles[] = 'font-size: 14px'; + $styles[] = 'font-weight: bold'; + $styles[] = 'color: #fff'; + $styles[] = 'z-index: 20000'; + $styles[] = 'user-select: none'; + $styles[] = 'pointer-events: none'; + $styles[] = 'text-align: center'; + $styles[] = 'border-radius: 2px'; + $message = '
' . htmlspecialchars($messagePreviewLabel) . '
'; + + $controller->content = str_ireplace('', $message . '', $controller->content); + } + + private function isInPreviewMode(Context $context): bool + { + return $context->hasAspect('frontend.preview') + && $context->getPropertyFromAspect('frontend.preview', 'isPreview', false); + } + + private function processWorkspacePreview(Context $context): bool + { + return $context->hasAspect('workspace') + && $context->getPropertyFromAspect('workspace', 'isOffline', false); + } + + private function getTypoScriptFrontendController(AfterCacheableContentIsGeneratedEvent $event): TypoScriptFrontendController + { + return $event->getController(); + } +} diff --git a/Configuration/Services.php b/Configuration/Services.php index 741846a5..32cb6e9b 100644 --- a/Configuration/Services.php +++ b/Configuration/Services.php @@ -19,6 +19,7 @@ use WebVision\WvDeepltranslate\Controller\Backend\AjaxController; use WebVision\WvDeepltranslate\Controller\GlossarySyncController; use WebVision\WvDeepltranslate\Event\Listener\GlossarySyncButtonProvider; +use WebVision\WvDeepltranslate\Event\Listener\RenderTranslatedFlagInFrontendPreviewMode; use WebVision\WvDeepltranslate\Event\Listener\UsageToolBarEventListener; use WebVision\WvDeepltranslate\Form\Item\SiteConfigSupportedLanguageItemsProcFunc; use WebVision\WvDeepltranslate\Form\User\HasFormalitySupport; @@ -163,6 +164,18 @@ ] ); + if ((new Typo3Version())->getMajorVersion() >= 12) { + // @todo Unnest this in next major when TYPO3 v11 support has been removed. + $services + ->set(RenderTranslatedFlagInFrontendPreviewMode::class) + ->tag( + 'event.listener', + [ + 'identifier' => 'deepltranslate-core/render-translated-flag-in-frontend-preview-mode', + ] + ); + } + /** * Check if WidgetRegistry is defined, which means that EXT:dashboard is available. * Registration directly in Services.yaml will break without EXT:dashboard installed! diff --git a/ext_localconf.php b/ext_localconf.php index caf5c0d1..eba794f7 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -40,8 +40,11 @@ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['checkModifyAccessList']['deepl'] = \WebVision\WvDeepltranslate\Hooks\TCEmainHook::class; - $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-all']['deepl-1675946132'] = - \WebVision\WvDeepltranslate\Hooks\DeeplPreviewFlagGeneratePageHook::class . '->renderDeeplPreviewFlag'; + if ($typo3version->getMajorVersion() < 12) { + // @todo Remove when TYPO3 v11 support is removed. + $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-all']['deepl-1675946132'] = + \WebVision\WvDeepltranslate\Hooks\DeeplPreviewFlagGeneratePageHook::class . '->renderDeeplPreviewFlag'; + } //xclass localizationcontroller for localizeRecords() and process() action $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\TYPO3\CMS\Backend\Controller\Page\LocalizationController::class] = [