4

I am trying to theme a Drupal 8 views. When I print the row variable using kint method I am getting below result. But I'm not sure how could I access the values in the object.

I tried below options. But nothing works.

  • {{ row._entity.title.value }}
  • {{ row.contents.get('title') }}
  • {{ row.contents.contents.title }}

Could anyone please help me how could I iterate through row data in views twig template.

enter image description here

4

There are two question. First how to get the title in the kint dump?

{{ row._entity.title.value }} should work, also {{ row._entity.gettitle }} or {{ row._entity.label }}, because getTitle() and label() are methods of the node object.

Second, could anyone please help me how could I iterate through row data in views twig template?

In Views two steps are needed, first get the field and then use the method getValue() on the field providing the result row as parameter:

{{ view.field.field_name.value(view.result[0]) }}
  • How exactly are you using getValue in your second example? – AlxVallejo Jan 26 '18 at 2:21
  • Twig .value() == PHP ->getValue(), you don't need the prefix get because Twig adds this automatically. For the title in the first example this is different, because nodes have a title field and .title would get the field object and not execute the method getTitle(), but in this case you can omit () if you don't have arguments. – 4k4 Jan 26 '18 at 7:25
1

If u want to edit classes for fields, u need to use template suggestion for

views-view-fields.html.twig

more u can see in documentation.

0

I do not think from the given screen shot section you can access the value. Because we can not access the protected value. You need to search other alternative.

Below links may help you

https://api.drupal.org/api/drupal/core!modules!views!views.theme.inc/function/template_preprocess_views_view_fields/8.2.x

How to get each field value in views-views-unformatted--view-machine-name.html.twig?

0

It seems drupal 8 changed the row to rows and added arrays before the row contents. I wanted to get the value of the row ex: title if I used the code above it showing null values. I also tried this {{kint(rows.content)}} to dig digger but still showing null values on {{kint(rows)}} works.

enter image description here

  • It seems like the kint output of this answer is from a different template than that of the question. The documentation at the top of each template lists the available variables. The top level templates provide rows, the field and fields templates a single row. – 4k4 Apr 9 '18 at 20:06
0

Whatever variable you are using to print out the Drupal/views/ResultRow may need [0] added so you can get a level down. In my case I was using {{ view.result }} and it took me forever to realize I needed to use {{ view.result[0].nid }} (to in my case print the node id)

{{ view.result[0]._entity.title.value }} might work for you here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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