2

I have a template views-view-fields.html.twig and created a view. I can access all the fields easily and also the link field. But it generates <a>. I want to extract link from this field.

I have searched SO and found some solution but they didn't work for me.

{{ fields.field_find_out_more_link[0]['#url'] }}

{{ fields.field_find_out_more_link.0['#url'] }}

{{ fields.field_find_out_more_link.url }}

Can anyone point me to right direction?

Omer
  • 1,727
  • 5
  • 28
  • 47
  • Possible duplicate of [Extract Url & Title from link field in Drupal 8?](https://stackoverflow.com/questions/34499554/extract-url-title-from-link-field-in-drupal-8) – Kevin Wenger Sep 06 '18 at 18:27

3 Answers3

2

This is working for me in views-view-fields.html.twig for the link field :

<a href="{{ fields.view_node.content|render|striptags|trim }}">text</a>
Sébastien Gicquel
  • 4,227
  • 7
  • 54
  • 84
0

Well, maybe I am a bit late but I think I found a proper answer to this.

In your View, you need to have a Link to Content field. Then, in the configuration of the field, you need to set to true the option "Show url as text". Finally, in your twig, you can set your button like:

<a href={{ fields.view_node.content|striptags }} class="btn">Yeah</a>

Hope this helps.

Zariweya
  • 295
  • 3
  • 13
0

Based on Sébastien Gicquel answare i used it like this in a views-view-fields.html.twig template with a custom link field:

  <a href="{{ fields.field_link.content|striptags|trim }}">
    {{ fields.field_image.content }}
    {{ fields.title.content }}
  </a>
zsd
  • 440
  • 5
  • 17