Ecodev / newsletter

TYPO3 extension to send newsletter
https://extensions.typo3.org/extension/newsletter/
25 stars 26 forks source link

[feature] Add hook in UriBuilder::buildFrontendUri() ? #134

Closed marvin-martian closed 7 years ago

marvin-martian commented 7 years ago

Hi I wanted to run the generated uri's through an URL shortener (tx_tinyurl). It would be nice if there was a hook in this method that I could pass and return the generated uri.

For example: Snippet

        // buildFrontendUriPostHook
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['newsletter']['buildFrontendUriPostHook'])) {
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['newsletter']['buildFrontendUriPostHook'] as $_classRef) {
                $_procObj = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($_classRef);
                $uri = $_procObj->buildFrontendUriPostHook($uri);
            }
        }

Integrated

    /**
     * Returns a frontend URI independently of current context, with or without extbase, and with or without TSFE
     * @param int $currentPid
     * @param string $controllerName
     * @param string $actionName
     * @param array $arguments
     * @return string absolute URI
     */
    public static function buildFrontendUri($currentPid, $controllerName, $actionName, array $arguments = [])
    {
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
        $extensionService = $objectManager->get(\TYPO3\CMS\Extbase\Service\ExtensionService::class);
        $pluginNamespace = $extensionService->getPluginNamespace(self::EXTENSION_NAME, self::PLUGIN_NAME);
        // Prepare arguments
        $arguments['action'] = $actionName;
        $arguments['controller'] = $controllerName;
        $namespacedArguments = [$pluginNamespace => $arguments];
        // Configure Uri
        $uriBuilder = self::getUriBuilder($currentPid);
        $uriBuilder->reset()
                ->setUseCacheHash(false)
                ->setCreateAbsoluteUri(true)
                ->setArguments($namespacedArguments)
                ->setTargetPageType(1342671779);
        $uri = $uriBuilder->buildFrontendUri();
        // For some reason the core set the backPath to something while building URI,
        // but it will somehow break the RecipientList editing in backend, so we unset it here
        // This was at least since TYPO3 7.6, maybe earlier, but not on TYPO3 8.0 anymore
        $pageRenderer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Page\PageRenderer::class);
        $pageRenderer->backPath = null;

        // buildFrontendUriPostHook
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['newsletter']['buildFrontendUriPostHook'])) {
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['newsletter']['buildFrontendUriPostHook'] as $_classRef) {
                $_procObj = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($_classRef);
                $uri = $_procObj->buildFrontendUriPostHook($uri);
            }
        }
        return $uri;
    }
PowerKiKi commented 7 years ago

IMHO this is out of scope of Newsletter and the whole point of using the UriBuilder is so that you can configure realurl (or whatever you use) to generate whatever URL structure fits your need. To me this is not the right place to manipulate URL. Maybe you should have a look at realurl doc ?

marvin-martian commented 7 years ago

Doh! you are right! tnx.