Drupal 9 - Output Views "Content: Link to Content" url to views-view-unformatted....html.twig template

1k views Asked by At

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

2

There are 2 answers

1
Sharklalu On

You said that {{row.content['#row']._entity.nid[0].value}} return your the id of your node.

So instead of

{{ path('entity.node.canonical', {'node': node.id}) }}

You should replace node.id by the id of the node in the row content :

{{ path('entity.node.canonical', {'node': row.content['#row']._entity.nid[0].value}) }} 

That's why your previous try failed, because in this context {{ node.id }} doesn't exist.

0
Jack L On

Here's what worked for this beginner courtesy of: link

Use this code in views-view-unformatted.WHATEVER.html.twig

{#
/**
 * @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()
 */
#}
{% if title %}
    <h3>{{ title }}</h3>
{% endif %}
{% for key,row in rows %}
    {% set row_classes = [
        default_row_class ? 'views-row',
    ] %}
  
         
<a href="{{ view.style_plugin.getField(key, 'view_node')|render|striptags|trim }}">Link to node</a>
         
{% endfor %}

Notice he uses the term "key" in {% for key,row in rows %} As this is being added to a link I added |render|striptags|trim to the end because with Twig Debug module enabled it was messing up the link by wrapping debug comments around it.

Hope it helps somebody else out! I also hope it's a decent/acceptable solution!?