Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

Question

How to get fields in views-view-unformatted.html.twig?

Solution

you can use this way to get any field in views-view-unformatted.html.twig :

{{ view.style_plugin.getField(KEY, 'FIELD_NAME') }}

Example

{#
/**
 * @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 %}
    <h3>{{ title }}</h3>
{% endif %}
{% for key,row in rows %}
    {% set row_classes = [
        default_row_class ? 'views-row',
    ] %}
    <div{{ row.attributes.addClass(row_classes) }}>
        <div class="field_video">
            {{ view.style_plugin.getField(key, 'field_video') }}
        </div>
        <div class="field_date">
            {{ view.style_plugin.getField(key, 'field_date') }}
        </div>
        <div class="field_audio">
            {{ view.style_plugin.getField(key, 'field_audio') }}
        </div>
    </div>
{% endfor %}
Tags :

Comments

Submitted by visva (not verified) on Fri, 05/08/2020 - 18:34 Permalink

Hi @codimth, I have passed some integer get the filed as you suggested, but i have got php notice error. Can you please guide what we will do for that? Ex: {% if view.style_plugin.getField(key, 'field_course_value') > 0 %} Notice: Object of class Drupal\Core\Render\Markup could not be converted to int in __TwigTemplate_739d0ced43e7294c6bf63287a82160170a0ccb56e8a06e4836d01eb016628d07->doDisplay() Thanks, Visva
Submitted by codimth on Mon, 05/11/2020 - 12:48 Permalink

@visva use twig filters to render just the values like this:

{% if view.style_plugin.getField(key, 'field_course')|render|striptags > 0 %}
    {{ view.style_plugin.getField(key, 'field_course')|render|striptags }}
{% endif %}
Submitted by sajith (not verified) on Thu, 09/24/2020 - 19:36 Permalink

But how to print the "link to content" field in view template ?
Submitted by codimth on Fri, 09/25/2020 - 12:52 Permalink

@sajith  just check if "Link to content" is checked in UI.

and in use views use like this:

<div class="field_title">
            {{ view.style_plugin.getField(key, 'field_title') }}
        </div>
Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon