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

2

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?

Wean answered 20/12, 2019 at 16:3 Comment(0)
W
13

since TYPO3 10.3 it can be done like this

<f:link.typolink parameter="{data.header_link}" parts-as="parts">
  {parts.title}
</f:link.typolink>

read more in the changelog documentation

these are all the parts u can use

<f:link.typolink parameter="123 _top news title" parts-as="parts">
   {parts.url}
   {parts.target}
   {parts.class}
   {parts.title}
   {parts.additionalParams}
</f:link.typolink>
Wyeth answered 21/5, 2021 at 1:44 Comment(0)
L
6
<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>
Labium answered 26/12, 2019 at 17:55 Comment(7)
Yes, thanks, Oliver, I know, that I can put the "Custom Link Text" between <f:link.typolink></f:link.typolink> but how can I get the title of the link, which I have set in the TYPO3 BE? <f:debug>{data.header_link}</f:debug> gives me t3://page?uid=123 - - "title" My question is: how can I get the title attribut to put it in your "Custom Link Text"Wean
The title part of the combined parameter string is supposed to be used as title attribute in typolink handling - thus, there is no out-of-the-box way in parsing and accessing that value. Using (new TypoLinkCodecService())->decode($headerLink)['title'] in a custom ViewHelper would allow to access that information (seems to be a nice and useful feature request).Labium
Thanks, for your detailed help. To include it in the original Viewhelper would be a nice and useful Feature indeed.Wean
Please see review.typo3.org/c/Packages/TYPO3.CMS/+/62769 as Feature for TYPO3 v10Labium
That's great. Therefore I love the whole opensource community thing, thanks a lot, Oliver.Wean
Oliver, I have a further question: is here any possibility to set the title of a content element as link text instead of the page title, if I link to the CE as an anchor link within the link wizard?Wean
Can you fix your code please?! In line: 12 -> return $typoLinkCodes->decode($this->renderChildren())[$part] ?? ''; it must be "$typoLinkCodec" (Codec, not Codes)Lithomarge

© 2022 - 2024 — McMap. All rights reserved.