1

My views block has a Link field named field_link, attached to a Content Type named products, and I'm trying access link field values(url and text) separately in views template views-view-unformatted.html.twig.

Following code gives the formatted html.

{{ row.content['#view'].style_plugin.render_tokens[ loop.index0 ]['{{ field_link }}'] }}

formatted html:

<a href="example.com/product-list" tabindex="-1">View Products</a>

But I want to access the link fields(url and text) value separately. Any help will be appreciated.

I am not able to check row variable via kint/dump as I am getting PHP Fatal error: Allowed memory size of 1073741824 bytes exhausted

1 Answer 1

3

The Easy way in Views UI add field_link 2 times.

in 1) Exclude from display, Select url only, and Show url as plain text. which will give you the url only.

enter image description here

in 2) Exclude from display, and REWRITE RESULTS -> Strip HTML tags which will give you the title only.

enter image description here

Then in a custom field you can use twig URL: {{ field_link }} Url Title: {{ field_link_1 }}

then in your custom template you can use them ...

Option 2) almost as easy create a custom field formatter based on Drupal\link\Plugin\Field\FieldFormatter\LinkSeparateFormatter.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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