-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
- Loading branch information
Showing
5 changed files
with
101 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
77 changes: 77 additions & 0 deletions
77
Classes/Event/Listener/RenderTranslatedFlagInFrontendPreviewMode.php
This file contains 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,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WebVision\WvDeepltranslate\Event\Listener; | ||
|
||
use TYPO3\CMS\Core\Context\Context; | ||
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; | ||
use TYPO3\CMS\Frontend\Event\AfterCacheableContentIsGeneratedEvent; | ||
use WebVision\WvDeepltranslate\Hooks\DeeplPreviewFlagGeneratePageHook; | ||
|
||
/** | ||
* Event listener to render the frontend preview flag information. | ||
* | ||
* TYPO3 v12+ only and this is the counter-part of the {@see DeeplPreviewFlagGeneratePageHook} for older TYPO3 versions. | ||
* https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.0/Breaking-97862-HooksRelatedToGeneratingPageContentRemoved.html | ||
*/ | ||
final class RenderTranslatedFlagInFrontendPreviewMode | ||
{ | ||
public function __invoke(AfterCacheableContentIsGeneratedEvent $event): void | ||
{ | ||
$controller = $this->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 = '<div id="deepl-preview-info" style="' . implode(';', $styles) . '">' . htmlspecialchars($messagePreviewLabel) . '</div>'; | ||
|
||
$controller->content = str_ireplace('</body>', $message . '</body>', $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(); | ||
} | ||
} |
This file contains 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
This file contains 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