Search code examples
typo3fluidtypo3-9.x

Check TYPO3 link type in fluid


I want to render a typolink in fluid, but i need to check if it is a download file (t3://file?uid=1), a page link (t3://page?uid=1) or an external link (https://www.abc.de _blank). Is there a way or viewhelper to check the linktype in fluid?

What i found were only via typoscript or a hacky way with VHS like

<f:if condition="{target -> v:math.round()} > 0">

It's for TYPO3 9.x


Solution

  • This is the ViewHelper i am using now:

    /**
     * A view helper for rendering the linktype
     *
     * = Examples =
     *
     * <code>
     * {nc:linkType(parameter: link)}
     * </code>
     * <output>
     * page, file, url, email, folder, unknown
     * </output>
     */
    class LinkTypeViewHelper extends AbstractViewHelper
    {
        use CompileWithRenderStatic;
    
        /**
         * Initialize arguments
         */
        public function initializeArguments()
        {
            $this->registerArgument('parameter', 'string', 'stdWrap.typolink style parameter string', true);
        }
    
        /**
         * @param array $arguments
         * @param \Closure $renderChildrenClosure
         * @param RenderingContextInterface $renderingContext
         * @return string Linktype (page, file, url, email, folder, unknown)
         */
        public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
        {
            $parameter = $arguments['parameter'];
    
            // workaround if parameter has _blank or other additional params
            $arr = explode(' ',trim($parameter));
            $firstparameter = $arr[0];
    
            $linkService = GeneralUtility::makeInstance(LinkService::class);
            $linkDetails = $linkService->resolve($firstparameter);
    
            return $linkDetails['type'];
    
        }
    }