Search code examples
fluidtypo3-9.x

How to set the link-title as Linktext with f:link.typolink viewhelper in TYPO3 9.5


I have build a custom content element with a link from field "header_link".

How can I set the title of that link as link text?

In the fluid template I use the link.typolink viewhelperin this way:

<f:link.typolink parameter="{data.header_link}" class="btn btn-primary" />

This results in

<a href="/page" title="link title" class="btn btn-primary">Page Title</a>

How do I set the link title as link text instead of the page title?


Solution

  • <f:link.typolink parameter="{data.header_link}" title="Title Attribute">
        Custom Link Text
    </f:link.typolink>
    

    results in

    <a href="/page" title="Title Attribute">
        Custom Link Text
    </a>
    

    Reference


    Update 1

    (updated from additional comments)

    Combined parameter string t3://page?uid=123 - - "title" (parameter target class title additional parameters) is supposed to only have affect on generating the anchor tag. There is no out-of-the-box way to use the title part as variable in a Fluid template.

    In order to do that, a custom ViewHelper would have to be created, e.g.

    class ParameterPartViewHelper extends AbstractViewHelper
    {
        public function initializeArguments()
        {
            $this->registerArgument('part', 'string', 'Parameter part to be extracted', true);
        } 
    
        public function render(): string
        {
            $part = $this->arguments['part'] ?? null;
            $typoLinkCodec = GeneralUtility::makeInstance(TypoLinkCodecService::class);
            return $typoLinkCodes->decode($this->renderChildren())[$part] ?? '';
        }
    

    That could be used in Fluid e.g. like this

    <f:link.typolink parameter="{data.header_link}" title="Title Attribute">
        {data.header_link -> my:parameterPart(part:'title')}
    </f:link.typolink>