0

A content type includes a link field link that accepts Link text.

A user inputs the URL /target with the Link text "Go to target".

How do I get twig to generate

   `<a href="/target">Go to target</a>`

I have tried what I've found by reading, including

  • {{ node.field_link }} (crashes)
  • {{ node.field_link|render }} (crashes)
  • {{ node.field_link.value }} (does nothing)
  • {{ node.field_link.0.url }} (displays /target but not as a link and ignores the link text)
  • {{ node.field_link.uri }} (displays internal:/target but not as a link and ignores the link text)

Seems like there must be a one-step way to accomplish this.

1
  • 1
    Doesn't twig just output this by default?
    – cchen
    Commented May 1, 2019 at 21:52

1 Answer 1

4

{{ node.field_link }} and {{ node.field_link|render } crashes because node.field_link of type FieldItemList not a rendered array.

Instead of using node variable use content variable like {{ content.field_link }} or even {{ content.field_link.0 }} it works.

2
  • 1
    Perfect. So simple, and yet I couldn't find it. Thanks! Commented May 1, 2019 at 22:44
  • 2
    Helped a lot! In my case that was link field in paragraphs, worked like: paragraph.field_link.0.url|render
    – betarobot
    Commented Oct 1, 2021 at 7:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.