Skip to content

Commit 8193ccc

Browse files
committed
[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 12 -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
1 parent b05460f commit 8193ccc

File tree

5 files changed

+101
-7
lines changed

5 files changed

+101
-7
lines changed

Build/phpstan/Core12/phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ parameters:
6060
count: 1
6161
path: ../../../Classes/Hooks/ButtonBarHook.php
6262

63-
-
64-
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$#"
65-
count: 1
66-
path: ../../../Classes/Hooks/DeeplPreviewFlagGeneratePageHook.php
67-
6863
-
6964
message: "#^Parameter \\#1 \\$uid of method WebVision\\\\Deepltranslate\\\\Core\\\\Domain\\\\Repository\\\\GlossaryEntryRepository\\:\\:findEntryByUid\\(\\) expects int, int\\|string given\\.$#"
7065
count: 1

Build/phpstan/Core12/phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ parameters:
1616
- ../../../.Build/*
1717
- ../../../Tests/Functional/Updates/Fixtures/Extension/test_extension/ext_emconf.php
1818
- ../../../Tests/Functional/Fixtures/Extensions/test_services_override/ext_emconf.php
19+
20+
typo3:
21+
contextApiGetAspectMapping:
22+
'frontend.preview': TYPO3\CMS\Frontend\Aspect\PreviewAspect
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WebVision\WvDeepltranslate\Event\Listener;
6+
7+
use TYPO3\CMS\Core\Context\Context;
8+
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
9+
use TYPO3\CMS\Frontend\Event\AfterCacheableContentIsGeneratedEvent;
10+
use WebVision\WvDeepltranslate\Hooks\DeeplPreviewFlagGeneratePageHook;
11+
12+
/**
13+
* Event listener to render the frontend preview flag information.
14+
*
15+
* TYPO3 v12+ only and this is the counter-part of the {@see DeeplPreviewFlagGeneratePageHook} for older TYPO3 versions.
16+
* https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.0/Breaking-97862-HooksRelatedToGeneratingPageContentRemoved.html
17+
*/
18+
final class RenderTranslatedFlagInFrontendPreviewMode
19+
{
20+
public function __invoke(AfterCacheableContentIsGeneratedEvent $event): void
21+
{
22+
$controller = $this->getTypoScriptFrontendController($event);
23+
$context = $controller->getContext();
24+
if (
25+
!$this->isInPreviewMode($context)
26+
|| $this->processWorkspacePreview($context)
27+
|| ($controller->config['config']['disablePreviewNotification'] ?? false)
28+
|| (
29+
isset($controller->page['tx_wvdeepltranslate_translated_time'])
30+
&& $controller->page['tx_wvdeepltranslate_translated_time'] === 0
31+
)
32+
) {
33+
// Preview flag must not be inserted. Return early.
34+
return;
35+
}
36+
37+
$messagePreviewLabel = ($controller->config['config']['deepl_message_preview'] ?? '')
38+
?: 'Translated with DeepL';
39+
40+
$styles = [];
41+
$styles[] = 'position: fixed';
42+
$styles[] = 'top: 65px';
43+
$styles[] = 'right: 15px';
44+
$styles[] = 'padding: 8px 18px';
45+
$styles[] = 'background: #006494';
46+
$styles[] = 'border: 1px solid #006494';
47+
$styles[] = 'font-family: sans-serif';
48+
$styles[] = 'font-size: 14px';
49+
$styles[] = 'font-weight: bold';
50+
$styles[] = 'color: #fff';
51+
$styles[] = 'z-index: 20000';
52+
$styles[] = 'user-select: none';
53+
$styles[] = 'pointer-events: none';
54+
$styles[] = 'text-align: center';
55+
$styles[] = 'border-radius: 2px';
56+
$message = '<div id="deepl-preview-info" style="' . implode(';', $styles) . '">' . htmlspecialchars($messagePreviewLabel) . '</div>';
57+
58+
$controller->content = str_ireplace('</body>', $message . '</body>', $controller->content);
59+
}
60+
61+
private function isInPreviewMode(Context $context): bool
62+
{
63+
return $context->hasAspect('frontend.preview')
64+
&& $context->getPropertyFromAspect('frontend.preview', 'isPreview', false);
65+
}
66+
67+
private function processWorkspacePreview(Context $context): bool
68+
{
69+
return $context->hasAspect('workspace')
70+
&& $context->getPropertyFromAspect('workspace', 'isOffline', false);
71+
}
72+
73+
private function getTypoScriptFrontendController(AfterCacheableContentIsGeneratedEvent $event): TypoScriptFrontendController
74+
{
75+
return $event->getController();
76+
}
77+
}

Configuration/Services.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use WebVision\Deepltranslate\Core\Service\ProcessingInstruction;
3333
use WebVision\Deepltranslate\Core\Service\UsageService;
3434
use WebVision\Deepltranslate\Core\Widgets\UsageWidget;
35+
use WebVision\WvDeepltranslate\Event\Listener\RenderTranslatedFlagInFrontendPreviewMode;
3536

3637
return function (ContainerConfigurator $containerConfigurator, ContainerBuilder $containerBuilder) {
3738
$typo3version = new Typo3Version();
@@ -161,6 +162,18 @@
161162
]
162163
);
163164

165+
if ((new Typo3Version())->getMajorVersion() >= 12) {
166+
// @todo Unnest this in next major when TYPO3 v11 support has been removed.
167+
$services
168+
->set(RenderTranslatedFlagInFrontendPreviewMode::class)
169+
->tag(
170+
'event.listener',
171+
[
172+
'identifier' => 'deepltranslate-core/render-translated-flag-in-frontend-preview-mode',
173+
]
174+
);
175+
}
176+
164177
/**
165178
* Check if WidgetRegistry is defined, which means that EXT:dashboard is available.
166179
* Registration directly in Services.yaml will break without EXT:dashboard installed!

ext_localconf.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
defined('TYPO3') or die();
44

55
(static function (): void {
6+
$typo3version = new \TYPO3\CMS\Core\Information\Typo3Version();
7+
68
//allowLanguageSynchronizationHook manipulates l10n_state
79
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][]
810
= \WebVision\Deepltranslate\Core\Hooks\AllowLanguageSynchronizationHook::class;
@@ -24,8 +26,11 @@
2426
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['checkModifyAccessList']['deepl']
2527
= \WebVision\Deepltranslate\Core\Hooks\TCEmainHook::class;
2628

27-
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-all']['deepl-1675946132'] =
28-
\WebVision\Deepltranslate\Core\Hooks\DeeplPreviewFlagGeneratePageHook::class . '->renderDeeplPreviewFlag';
29+
if ($typo3version->getMajorVersion() < 12) {
30+
// @todo Remove when TYPO3 v11 support is removed.
31+
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-all']['deepl-1675946132'] =
32+
\WebVision\WvDeepltranslate\Hooks\DeeplPreviewFlagGeneratePageHook::class . '->renderDeeplPreviewFlag';
33+
}
2934

3035
//xclass localizationcontroller for localizeRecords() and process() action
3136
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\TYPO3\CMS\Backend\Controller\Page\LocalizationController::class] = [

0 commit comments

Comments
 (0)