10

I'm having trouble doing something that I think should be relatively simple drupal 8 views.

I have a content type called Countries. I would like to display the 3 latest country nodes on my homepage in a views block. Each country is displayed with the class "views-row" on the container div. I am using views--view--unformatted--countries--block_1.tpl to template the output.

I would like to output something like the following markup:

<a class="view-row-1" href="/link/to/node">
    <img src="source-of-teaser-image.png">
    <h3>Title of node</h3>
</a>

<a class="view-row-2" href="/link/to/node">
    <img src="source-of-teaser-image.png">
    <h3>Title of node</h3>
</a>

<a class="view-row-3" href="/link/to/node">
    <img src="source-of-teaser-image.png">
    <h3>Title of node</h3>
</a>

The problem I'm having is accessing individual fields in the template. If I use a view mode, I can access individual fields. If I select "show fields" in the view, I can add a field for "view result counter" and "path", which would allow me to add the "view-row-N" class and link the a tag to the node, but I can't get access to the fields individually. I have the {{row.content}} variable, but any attempt to dig further into the variable (eg row.content.field_name) gives me nothing and calling a {{dump(row.content)}} crashes the website.

I can't output this as a view mode for 2 reasons. I don't have access to the "view result counter" or "path" fields in a view mode and, even if I had these variables, some fields would be nested inside others (The image and title are nested inside the )

I feel this should really be as simple as

<a class="view-row-{{ row.content.view_result_counter }}" href="{{ row.content.path }}">

etc but I've tried everything I can think of. Am I completely on the wrong path? Twig and I are not getting along so far...

Kaizen9001
  • 510
  • 1
  • 4
  • 11

6 Answers6

18

I have figured a way using kint.

Inside your views-view-unformatted.html.twig use the following code to display your individual fields:

{% for row in rows %}

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

{% endfor %}
Ibrahim Samir
  • 281
  • 2
  • 5
  • +1 upvote on this answer and the question - this answer (and question) can help those who need markup (e.g. ` – therobyouknow Oct 17 '18 at 20:54
  • @ibrahim-samir How can we get the URL instead of the whole style rendering using this code? In this code, i want to get the value of this field `field_slack_team_link` in the `values` – Harikris Aug 29 '23 at 16:04
0

Using content/view mode works in your case.

1/ In "views-view-unformatted.html.twig" you can use "loop.index" in the "for" loop to get index of the current row and add it to the "row_classes" variable.

2/In the twig template for the node you can use something like :

<a href="{{ path('entity.node.canonical', {'node': node.id}) }}">
    {{ content.field_img }}
    {{ node.getTitle()}}
</a>

3/You will probably get extra html but you can get rid of most of it by overriding relevant templates.

progzy
  • 607
  • 1
  • 4
  • 5
0

I am not sure if it's what you expect, but you can override field template:

  1. Enable Twig debugging: https://www.drupal.org/node/1903374
  2. Inspect your page in browser (if site is in debug mode there are comments in html with paths to templates for each element). For example this is field template: core/themes/stable/templates/views/views-view-fields.html.twig
  3. Copy required template into your theme folder
  4. Add or modify markup.

You can debug template using {{ dump(variable) }} or {{ kint(variable) }}

fadehelix
  • 201
  • 2
  • 14
0

In your case you should be using views-view-fields--countries--block_1.html.twig instead of views-view-unformatted--countries--block_1.html. twig file.

Views fields template will iterate on all rows of result. Your views-view-fields--countries--block_1.html.twig should contain below code -

<div>
  <a class="view-row" href="{{variable-to-print-path }}">
   <img src="{{ image-path }}">
   <h3>{{ fields.title.content</h3>
  </a>
</div>

You can check the fields array by using kint function like - {{ kint(fields) }}

Check for image path and anchor path variable from output of kint function.

If you want to do this in views-view-unformatted--countries--block_1.html. twig, then you can access the field values like below -

{% for row in rows %}
  {% set photo = file_url(row['content']['#row']._entity.field_page_photo.entity.fileuri) %}          
  <li><img src={{ photo }} class="img-responsive img-circle"></li>
{% endfor %}
Renuka Kulkarni
  • 335
  • 2
  • 8
0

The solution is simple use the views fields template.

I created a new template under themes/templates folder:

views-view-fields--VIEW-NAME.html.twig

And now you can access your fields values like this:

{{ fields.field_NAME.content }}
Viswa
  • 106
  • 6
0

If you want to access Individual fields in Views Twig Template along with Row Information, Override Views Fields Template in your custom theme :

views-view-fields--VIEW-NAME.html.twig

To get the row index in the same file, Use :

{{ row.index }}