Score:0

How can I get the href value from a link?

ua flag

I have a link I render in a template file with the following markup.

{{ item.caption.link }}

I want to show only the href value. None of the following worked for me.

{{ item.caption.link[0]['#url'] }}
{{ item.caption.link.0.url }}
{{ item.caption.link[0]['#url']|render }}
Score:0
in flag

I think it might be better if you pass just href value as a variable to Twig and not the complete HTML markup.

Put HTML in Twig and just fill it with the values (href value in that case) you need.

developer avatar
ua flag
This is generated dynamic by a module and I am override the twig template in theme
Adamssef avatar
in flag
It's hard to say without knowing what this variable holds. Can you inspect it with Xdebug or sth?
Score:0
gr flag

First look for the available attributes with this tutorial on XDebug.

Another method to inspect a variable is to use the dd() function. See this question to install it. Put the following in your template, it will print all the available attributes of your link :

{{ dd(item.caption.link) }}

Try the following :

{{ item.caption.link.0['#url'] }}

If this doesn't work, you should access at least to the uri. See this question to get an URL from the URI.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.