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
    Doesn't twig just output this by default? – cchen May 1 at 21:52
1

{{ 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.

  • Perfect. So simple, and yet I couldn't find it. Thanks! – Marshall Morrise May 1 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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