Score:0

How to get the url of a link field in field.html.twig

in flag

Inside the field--link.html.twig file, I'm trying to output the url from a link field like so:

{% for item in items %}
  <div{{ attributes.addClass(classes) }}><a class="button" href="{{ item.content.uri }}">Apply Now</a></div>
{% endfor %}

I've tried the following to get the url, and nothing seems to work.

  • item.content.uri,

  • item.content.uri,

  • item.content.url,

  • item.content.0.url,

  • item.content.url | render,

  • item.content.0['#url'], etc ...

How can I get the url from my link field?

Thanks

usmanjutt84 avatar
in flag
Have you tried like `link['#url'].uri` this?
in flag
This seems to be the answer. {{ item.content['#url'].uri }} May I ask where this information is kept?
leymannx avatar
ne flag
Does this answer your question? [How to get the valid URL of a Link field from within a Twig template?](https://drupal.stackexchange.com/questions/199262/how-to-get-the-valid-url-of-a-link-field-from-within-a-twig-template)
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.