2
votes

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

3
Why not use <f:link.typolink parameter="...">? This can handle all 3 types.Rudy Gnodde
Yes, but i need a condition, because i will render a download differently than a page or external page. f:link.typolink is no option.kimomat
You could already enrich the data model by an additional flag. Always thinking of a ViewHelper in first place, not only tends contradict the separation of MVC. It often also causes more work to do, just because of not applying the advantages of MVC.Blcknx

3 Answers

3
votes
$linkService = $this->objectManager->get(LinkService::class);
$result = $linkService->resolve($linkValue);

That could help you in a custom ViewHelper

Possible Values for $linkValue:

  • t3://page?uid=1 => [string (pageuid), 'page']
  • [email protected] => [string (email), 'email']
  • https://typo3.org => [string (url), 'url']
  • t3://file?uid=226 => [TYPO3\CMS\Core\Resource\File, 'file']

$result returns an array. Every case has the argument "type". Depending on the type, another value or object is returned. I have listed this above.

The class is available from TYPO3 Version 8.

2
votes

You could also check the linktype with the vhs extension, e.g. to set a different target:

{namespace v=FluidTYPO3\Vhs\ViewHelpers}
...
<f:variable name="target">_self</f:variable> 
<v:condition.string.contains haystack="{url}" needle="t3://file?uid">
  <f:variable name="target">_blank</f:variable>         
</v:condition.string.contains>
<v:condition.string.contains haystack="{url}" needle="http">
  <f:variable name="target">_blank</f:variable>         
</v:condition.string.contains>
<v:condition.string.contains haystack="{url}" needle="www">
  <f:variable name="target">_blank</f:variable>
</v:condition.string.contains> 

<f:link.typolink parameter="{url}" target="{target}">the link</f:link.typolink>
1
votes

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

    }
}