From 440f9ae1b4d166fabc65d089d4c3a74804b3b3f9 Mon Sep 17 00:00:00 2001 From: Markus Hofmann Date: Thu, 15 May 2025 18:40:21 +0200 Subject: [PATCH] [TASK] Refactor JavaScript implementation The issue on TYPO3 13 having the button label writing outside the borders made a refactoring and regeneration of the localization.ts necessary. The labels were rewritten as CDATA to allow line breaks, getting rid of the too long labels. Together with TYPO3 Core standardized TSconfig for localizations the overridden JavaScript is more lightweight and doesn't need the AjaxRoute anymore. Therefore the AjaxRoutes.php and AjaxController.php are removed. In addition to Core functionality, the DeepL allow translateion now can be enabled/disabled by the following setting in Page TSConfig: ``` # Enabled (Default) mod.web_layout.localization.enableDeeplTranslate = 1 # Disabled mod.web_layout.localization.enableDeeplTranslate = 0 ``` This could be overridden in User TSconfig, too: ``` # Enabled (Default) page.mod.web_layout.localization.enableDeeplTranslate = 1 # Disabled page.mod.web_layout.localization.enableDeeplTranslate = 0 ``` --- Classes/Configuration.php | 17 ++++++++ Classes/ConfigurationInterface.php | 2 + Classes/Controller/Backend/AjaxController.php | 39 ------------------- .../Listener/RenderLocalizationSelect.php | 8 +++- .../DeeplTranslateAllowedViewHelper.php | 22 ++++++++--- Configuration/Backend/AjaxRoutes.php | 17 -------- 6 files changed, 43 insertions(+), 62 deletions(-) delete mode 100644 Classes/Controller/Backend/AjaxController.php delete mode 100644 Configuration/Backend/AjaxRoutes.php diff --git a/Classes/Configuration.php b/Classes/Configuration.php index 087c5423..cb155749 100644 --- a/Classes/Configuration.php +++ b/Classes/Configuration.php @@ -5,6 +5,7 @@ namespace WebVision\Deepltranslate\Core; use Symfony\Component\DependencyInjection\Attribute\AsAlias; +use TYPO3\CMS\Backend\Utility\BackendUtility; use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException; use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException; use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; @@ -30,4 +31,20 @@ public function getApiKey(): string { return $this->apiKey; } + + /** + * Checks translation allowed against Page TSconfig from settings + * + * @param int $pageId + * @return bool + * @throws \JsonException + */ + public function isDeeplTranslationAllowedOnPage(int $pageId): bool + { + $localizationConfiguration = BackendUtility::getPagesTSconfig($pageId)['mod.']['web_layout.']['localization.'] ?? []; + if ((bool)($localizationConfiguration['enableDeeplTranslate'] ?? true) === false) { + return false; + } + return true; + } } diff --git a/Classes/ConfigurationInterface.php b/Classes/ConfigurationInterface.php index f08874de..9bbe1019 100644 --- a/Classes/ConfigurationInterface.php +++ b/Classes/ConfigurationInterface.php @@ -13,4 +13,6 @@ interface ConfigurationInterface { public function getApiKey(): string; + + public function isDeeplTranslationAllowedOnPage(int $pageId): bool; } diff --git a/Classes/Controller/Backend/AjaxController.php b/Classes/Controller/Backend/AjaxController.php deleted file mode 100644 index 316d1a43..00000000 --- a/Classes/Controller/Backend/AjaxController.php +++ /dev/null @@ -1,39 +0,0 @@ -configuration = $configuration; - } - - /** - * check deepl Settings (url,apikey). - */ - public function checkExtensionConfiguration(ServerRequestInterface $request): JsonResponse - { - $configurationStatus = [ - 'status' => true, - 'message' => '', - ]; - if ($this->configuration->getApiKey() == null) { - $configurationStatus['status'] = false; - $configurationStatus['message'] = 'Deepl settings not enabled'; - } - - return new JsonResponse($configurationStatus); - } -} diff --git a/Classes/Event/Listener/RenderLocalizationSelect.php b/Classes/Event/Listener/RenderLocalizationSelect.php index 7565e9a9..26c3225c 100644 --- a/Classes/Event/Listener/RenderLocalizationSelect.php +++ b/Classes/Event/Listener/RenderLocalizationSelect.php @@ -7,6 +7,7 @@ use Psr\EventDispatcher\EventDispatcherInterface; use TYPO3\CMS\Backend\Controller\Event\RenderAdditionalContentToRecordListEvent; use TYPO3\CMS\Core\Site\Entity\Site; +use WebVision\Deepltranslate\Core\ConfigurationInterface; use WebVision\Deepltranslate\Core\Event\RenderLocalizationSelectAllowed; use WebVision\Deepltranslate\Core\Form\TranslationDropdownGenerator; @@ -14,13 +15,18 @@ final class RenderLocalizationSelect { public function __construct( private readonly TranslationDropdownGenerator $generator, - private readonly EventDispatcherInterface $eventDispatcher + private readonly EventDispatcherInterface $eventDispatcher, + private readonly ConfigurationInterface $configuration ) { } public function __invoke(RenderAdditionalContentToRecordListEvent $event): void { $request = $event->getRequest(); + $currentPageId = (int)($request->getQueryParams()['id'] ?? 0); + if (!$this->configuration->isDeeplTranslationAllowedOnPage($currentPageId)) { + return; + } // Check, if some event listener doesn't allow rendering here. // For use cases see Event $renderingAllowedEvent = $this->eventDispatcher->dispatch(new RenderLocalizationSelectAllowed($request)); diff --git a/Classes/ViewHelpers/Be/Access/DeeplTranslateAllowedViewHelper.php b/Classes/ViewHelpers/Be/Access/DeeplTranslateAllowedViewHelper.php index e9bea19d..2db996e4 100644 --- a/Classes/ViewHelpers/Be/Access/DeeplTranslateAllowedViewHelper.php +++ b/Classes/ViewHelpers/Be/Access/DeeplTranslateAllowedViewHelper.php @@ -5,19 +5,31 @@ namespace WebVision\Deepltranslate\Core\ViewHelpers\Be\Access; use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext; use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper; use WebVision\Deepltranslate\Core\Access\AllowedTranslateAccess; +use WebVision\Deepltranslate\Core\ConfigurationInterface; +/** + * @internal This ViewHelper is marked internal and only to be used within + * the DeepL translate packages and therefore no public API. + */ final class DeeplTranslateAllowedViewHelper extends AbstractConditionViewHelper { - public function initializeArguments(): void - { - parent::initializeArguments(); - } - public static function verdict(array $arguments, RenderingContextInterface $renderingContext): bool { + $deeplConfiguration = GeneralUtility::makeInstance(ConfigurationInterface::class); + if ($deeplConfiguration->getApiKey() === '') { + return false; + } + /** @var RenderingContext $renderingContext */ + $currentPageId = (int)($renderingContext->getRequest()?->getQueryParams()['id'] ?? 0); + // set default to true avoiding breaking behaviour issues + if (!$deeplConfiguration->isDeeplTranslationAllowedOnPage($currentPageId)) { + return false; + } if (self::getBackendUserAuthentication()->check('custom_options', AllowedTranslateAccess::ALLOWED_TRANSLATE_OPTION_VALUE)) { return true; } diff --git a/Configuration/Backend/AjaxRoutes.php b/Configuration/Backend/AjaxRoutes.php deleted file mode 100644 index 8c90ac41..00000000 --- a/Configuration/Backend/AjaxRoutes.php +++ /dev/null @@ -1,17 +0,0 @@ - [ - 'path' => '/deepl/check-configuration', - 'target' => AjaxController::class . '::checkExtensionConfiguration', - ], -];