Search code examples
typo3fluidtypo3-11.x

TYPO3 - f:link.typolink with additionalAttributes and a condition?


I am looking for a solution, how to include a query in additionalAttributes?

Static:

<f:link.typolink parameter="{field.referenceLink}" title="{field.referenceTitel} - {field.referenceText}" additionalAttributes="{rel:'nofollow'}">
    Link
</f:link.typolink>

condition:

<f:variable name="nofollow"><f:if condition="{field.referenceLinkNofollow}"><f:then>{rel:'nofollow'}</f:then><f:else>{rel:'dofollow'}</f:else></f:if></f:variable>

next Test:

<f:section name="nofollow">
    <f:spaceless><f:if condition="{field.referenceLinkNofollow}"><f:then>{rel:'nofollow'}</f:then><f:else>{rel:'dofollow'}</f:else></f:if></f:spaceless>
</f:section>

Dont Work like this:

<f:link.typolink parameter="{field.referenceLink}" title="{field.referenceTitel} - {field.referenceText}" additionalAttributes="{nofollow}">
    Link
</f:link.typolink>

Error:
The argument "additionalAttributes" was registered with type "array", but is of type "string" in view helper "TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper".


Solution

  • One option could be:

    <f:variable name="nofollow" value="{f:if(condition: '{data.no_follow}', then: 'nofollow', else: '')}" />
    <f:link.typolink parameter="https://example.com" title="Title" additionalAttributes="{rel: '{nofollow}'}">
      Link
    <f:link.typolink>
    

    Let's assume that the variable {data.no_follow} represents the setting that backend users can change in the page properties ("Follow this page").

    The first line sets the variable nofollow in the Fluid template to either the value 'nofollow' or to '' (empty), depending on the page property setting. I have used the inline notation of the If-ViewHelper to achieve this. The variable {nofollow} is then used in the LinkTypolink-ViewHelper as the value for the rel= attribute under additionalAttributes.

    As empty values are dropped, the rel= attribute is only shown if it has value.