C
C
curious_dev2020-10-01 17:34:27
Drupal
curious_dev, 2020-10-01 17:34:27

How to display a summary for a field in a block-view theming file (block--views-....html.twig)?

All actions take place on a site with Drupal 8 installed on it. There is a block on the main page, which is a view that displays the 3 latest news of the site. Each news has 3 fields:

  • Publication date (field_publish_date).
  • Title (field_title).
  • News text (field_body).

5f75e890a592e872469824.jpeg
The view is set up in such a way that the value of the body_value column with all sorts of extra html tags and pictures is taken from the database table, which contains the text of the news, but a brief announcement - the value of the body_summary column. This is done by selecting in the settings of the corresponding field from the select "Formatter" the option "Summary or trimmed text":
5f75e8b941bce207962779.jpeg
Now there is a need to reverse this block, and for this purpose I created a theming file block--views-block--news- block-1.html.twig . In it, I can access the {{ content }} variable, which contains all of the above fields. But I need to access each field individually, not the whole content. After trying a bunch of non-working options (like {{ content.or {{ content.field_body[0].value }}), found the only worker in my case:
{% for row in content['#view'].result %}
  {{ content['#view'].field['field_publish_date'].value(row) }}
  {{ content['#view'].field['field_title'].value(row) }}
  {{ content['#view'].field['field_body'].value(row) }}
{% endfor %}

Everything looks great and I can wrap the fields in separate html tags. The problem is that content['#view'].field['field_body'].value(row) gives me the contents of the body_value column from the database, and I need to access the body_summary column. I tried a bunch of different options, but none worked. Can you please tell me how can I display the value of the body_summary column in this twig file?
So far, I figured out for sure that my way of accessing fields gets their raw values ​​\u200b\u200b(that is, before any processing). Perhaps there is some way to get their final values? Or a way to refer to the same "raw" data, but to a specific column from the database (something like {{ content.field_body.0.body_summary }} )?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
andead, 2020-10-01
@andead

If you need to change the layout of the field, then you need to redefine the Views field template, and not the block template. Enable twig debug and it will show the name of all templates used.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question