1

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).

  • 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. – 4k4 Apr 9 at 18:52
  • 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. – Marshall Morrise Apr 9 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. – Marshall Morrise Apr 9 at 21:00
1

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?

  • 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.) – Marshall Morrise Apr 11 at 4:02

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.