By Marshall Morrise


2019-04-09 18:02:32 8 Comments

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

1 comments

@4k4 2019-04-09 21:39:39

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?

@Marshall Morrise 2019-04-11 04:02:10

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

Related Questions

Sponsored Content

2 Answered Questions

How can I pass variables from views template to node template?

1 Answered Questions

Arrange formatted fields in Views row using a template

  • 2014-12-19 11:38:56
  • deinqwertz
  • 457 View
  • 2 Score
  • 1 Answer
  • Tags:   7 views entities

1 Answered Questions

[SOLVED] Numbering rows of a table-view

2 Answered Questions

[SOLVED] Weird behaviour when customising views-view-unformatted.html.twig with grouping

  • 2018-08-17 07:27:25
  • millionleaves
  • 190 View
  • 0 Score
  • 2 Answer
  • Tags:   views theming

4 Answered Questions

[SOLVED] How can I add custom classes to each view's generated row?

  • 2017-07-17 21:25:34
  • Mojtaba Reyhani
  • 2693 View
  • 1 Score
  • 4 Answer
  • Tags:   views theming 8

1 Answered Questions

[SOLVED] How can I render a result row using the Views API

  • 2012-04-07 06:13:54
  • Aidan
  • 6455 View
  • 6 Score
  • 1 Answer
  • Tags:   views

2 Answered Questions

[SOLVED] Change Drupal Views Rows before they were rendered

  • 2015-03-27 12:07:53
  • guest123
  • 150 View
  • 0 Score
  • 2 Answer
  • Tags:   views theming hooks

2 Answered Questions

Render a Views table row ID using a field as the ID value

  • 2015-02-12 16:42:26
  • Scott859
  • 187 View
  • 1 Score
  • 2 Answer
  • Tags:   views

1 Answered Questions

[SOLVED] How is this themed when using views

  • 2014-10-27 14:50:47
  • Alex.Barylski
  • 22 View
  • 0 Score
  • 1 Answer
  • Tags:   views theming

2 Answered Questions

[SOLVED] Create views field template override for table style

  • 2012-02-20 13:00:26
  • Perisdr
  • 625 View
  • 0 Score
  • 2 Answer
  • Tags:   views theming

Sponsored Content