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', - ], -];