2

I use a table format view to display a list of articles article-list using draggable views to allow editors to order the list. I customize the content of rows depending on a boolean field is_header and a formatted text field title_override. I have sub-themed views-view-table--article-list.html.twig to accomplish this. It works well enough.

In the template, I test the truth of is_header and the non-emptiness of title_override as follows:

    {% if row.columns.field_is_header|render|striptags|trim %}

and

    {% if row.columns.field_title_override|render|striptags|trim %}

These tests yield the proper results, but they make the template very slow! Anything less than render|striptags|trim doesn't work.

QUESTION:

Is there a way to directly access the raw data values of these variables to avoid the render|striptags|trim overhead?

I've seen suggestions to use something like row._entity.field_is_header, but this way of using _entity seems to work only in templates for the HTML or Unformatted view styles. Seems like there should be an easy way to do this.

As a note, switching to using an HTML or Unformatted style doesn't seem possible because AFAIK draggable views can be set up only with the table format.

ANSWER:

Thanks to 4k4 for the answer to this question (see below). To apply his/her general answer to my specific question, here is a snippet of the twig script I ended up with:

{% for key, row in rows %}
  {% if view.field.field_is_header.value(view.result[key]) %}
    <conditional stuff>
  {% endif %}
  {% if view.field.field_title_override.value(view.result[key]) %}
    <stuff when the override is non-empty>
  {% else %}
    <stuff when the override is empty>
  {% endif %}
{% endfor %}

Note that rows is an array normally passed to the views-view-table.html.twig template, as documented in the original template's header comments. Note also that the availability of the view array is NOT documented in the original template's header comments (not that it would have saved me without 4k4's help).

4
  • 2
    You can't theme views field like entity fields. Most times formatting views fields in UI is the better approach. If you need a boolean field only to configure the format of other fields add the boolean field first, exclude it from display and use it as replacement token to rewrite the result of other fields.
    – 4uk4
    Commented Apr 9, 2019 at 18:52
  • See drupal.stackexchange.com/questions/218022/…
    – 4uk4
    Commented Apr 9, 2019 at 19:03
  • Thanks (and an upvote) @4k4. To simplify, I left out some details, one of which is that the boolean is_header is used for other purposes in the template. At your suggestion, I did the field formatting in Rewrite Results for of one of the fields (I assume that's what you were suggesting). It works well and seems better than what I was doing. I'll try figure out a way to handle is_header differently. Commented Apr 9, 2019 at 19:27
  • Still seeking an answer to this. It's hard to imagine that the raw, original data isn't available somewhere in the array passed to the twig template. But several hours of reading and trial-and-error have yielded no success. Commented Apr 9, 2019 at 21:00

1 Answer 1

2

You can try to apply getValue() of the field handler on the result row, for example:

{% if view.field.field_name.value(view.result[key]) %}

Edit:

In a views table template row is not of the type ResultRow, so you need to get it directly from the view object using key of the loop {% for key, row in rows %}.

See these questions for different views templates: How can I get data from ResultRow object in views template and How to preprocess view field?

4
  • YES! This is the answer I was seeking. Thanks, @4k4. I have accepted this as the correct answer and have added it to my question so anyone who comes looking can find it easily. (I always wonder how you or others come to know this kind of stuff.) Commented Apr 11, 2019 at 4:02
  • You answered this a long time ago. I'm still using it. Thanks! Can you tell me how to get the node id for the node associated with a row? Is the nid also in the view object for the views table template row? Commented Nov 7, 2021 at 5:57
  • You can use the same code on the field nid, if configured in the field list of the View. Or get the _entity property from the ResultRow, if it is available: {{ view.result[key]._entity.id }}
    – 4uk4
    Commented Nov 7, 2021 at 9:35
  • Thanks. It's the second way you show that I couldn't figure out. The only examples I found were for rows of type ResultRow. Commented Nov 7, 2021 at 20:38

Your Answer

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

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