I am trying to output the url alias from Views "Content: Link to Content" in a Twig template of the format views-view-unformatted....html.twig. The url alias outputs successfully in the Views preview similar to /path1/article. Forums suggest {{ fields.view_node.content }} should work but it doesn't in my case.
views-view-unformatted....html.twig is as below
{#
/**
* @file
* Theme override to display a view of unformatted rows.
*
* Available variables:
* - title: The title of this group of rows. May be empty.
* - rows: A list of the view's row items.
* - attributes: The row's HTML attributes.
* - content: The row's content.
* - view: The view object.
* - default_row_class: A flag indicating whether default classes should be
* used on rows.
*
* @see template_preprocess_views_view_unformatted()
*/
#}
{% for row in rows %}
{%
set row_classes = [
default_row_class ? 'views-row',
]
%}
{{row.content['#row']._entity.title[0].value}} ***returns value for title
/node/{{row.content['#row']._entity.nid[0].value}}" ***returns nid
{{ fields.view_node.content }} ***doesn't work
{{ path('entity.node.canonical', {'node': node.id}) }} ***crashes page
{% endfor %}
I've tried variations like:
{{row.content['#row']._entity.url[0].value}}
{{row.content['#row']._entity.view_node[0].value}}
{{row.content['#row']._entity.alias[0].value}}
with no success.
It looks like my template doesn't have a fields variable as mentioned at this thread so how would I go about enabling one? I use the Drupal Barrio theme
Would love some help. Thanks
Added:
When I do a var dump I see"
+field: &52 array:7 [▼
"view_node" => Drupal\views\Plugin\views\field\EntityLink {#9889 ▶}
"title" => Drupal\views\Plugin\views\field\EntityField {#10494 ▶}
"nid" => Drupal\views\Plugin\views\field\EntityField {#10357 ▶}
"body" => Drupal\views\Plugin\views\field\EntityField {#10827 ▶}
"field_featured_image" => Drupal\views\Plugin\views\field\EntityField {#10503 ▶}
"field_subheading" => Drupal\views\Plugin\views\field\EntityField {#10375 ▶}
"field_news_tags" => Drupal\views\Plugin\views\field\EntityField {#10824 ▶}
]
The link relates to view mode is an Entity Link instead of an Entity Field.
I tried all these variation but none rendered the url/alias
#view
{{row.content['#view'].field.view_node[0].value}}
{{row.content['#view'].field.view_node[0].content}}
{{row.content['#view'].field.view_node.content}}
{{row.content['#view'].field.view_node.value}}
{{row.content['#view'].link.view_node[0].content}}
{{row.content['#view'].link.view_node.content}}
#row
{{row.content['#row'].field.view_node[0].value}}
{{row.content['#row'].field.view_node[0].content}}
{{row.content['#row'].field.view_node.content}}
{{row.content['#row'].field.view_node[0].value}}
{{row.content['#row'].link.view_node[0].content}}
{{row.content['#row'].link.view_node.content}}
.tried fields. (plural) too with no luck
You said that {{row.content['#row']._entity.nid[0].value}} return your the id of your node.
So instead of
You should replace node.id by the id of the node in the row content :
That's why your previous try failed, because in this context {{ node.id }} doesn't exist.