5

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

0

6 Answers 6

5

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]) }}
2
  • How exactly are you using getValue in your second example?
    – AlxVallejo
    Jan 26, 2018 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.
    – 4uk4
    Jan 26, 2018 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

1
  • 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.
    – 4uk4
    Apr 9, 2018 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

-1

{# The template name is "views-view--agenda.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 %}

{{ title }}

{% endif %}

{% for row in rows %} {% set row_classes = [ default_row_class ? 'views-row', ] %} {% endfor %}

<div{{ row.attributes.addClass(row_classes) }}>

{# for each row  ('agenda' is the content-type, on which we do the view) #}

{% set items = view.result %}  
 {% for item in items %}

        {{ item._entity.field_fist_name.value }} 

        {{ item._entity.field_name.value }} 

        <img src="{{ file_url(item._entity.field_image.entity.uri.value|image_style('medium')) }}"/>  

        {{ item._entity.field_job.value }}

        {{ item._entity.field_selected_list.value }} 

        {{ item._entity.field_agency_name.value }} 

        {{ item._entity.field_description.value }}

        {{ item._entity.field_date_text_format.value }} 

        {{ item._entity.field_hour_text_format.value }}

        {# {{ item._entity.field_weight.value }} #}
        


{% endfor %}

Your Answer

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

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