4

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?

m4a
  • 131
  • 1
  • 9

2 Answers2

9

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>
HenryAveMedi
  • 131
  • 1
  • 5
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>
Oliver Hader
  • 4,093
  • 1
  • 25
  • 47
  • 1
    Yes, thanks, Oliver, I know, that I can put the "Custom Link Text" between but how can I **get** the title of the link, which I have set in the TYPO3 BE? {data.header_link} 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" – m4a Dec 26 '19 at 18:40
  • 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). – Oliver Hader Dec 26 '19 at 19:03
  • Thanks, for your detailed help. To include it in the original Viewhelper would be a nice and useful Feature indeed. – m4a Dec 26 '19 at 22:19
  • 2
    Please see https://review.typo3.org/c/Packages/TYPO3.CMS/+/62769 as Feature for TYPO3 v10 – Oliver Hader Feb 22 '20 at 16:03
  • That's great. Therefore I love the whole opensource community thing, thanks a lot, Oliver. – m4a Feb 24 '20 at 09:28
  • 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? – m4a Apr 23 '20 at 06:43
  • Can you fix your code please?! In line: 12 -> return $typoLinkCodes->decode($this->renderChildren())[$part] ?? ''; it must be "$typoLinkCodec" (Codec, not Codes) – Ruwen Gastrock Dec 01 '21 at 15:57