5

I have a view and added the title as a field.

For example: How do I print the title?

In Drupal 7 it was as simple as

views-view-fields--[viewname].tpl.php

<?php print $fields["title"]->content; ?>

How do we do this in Drupal 8?

I have created views-view-fields--[viewname].html.twig

I thought it might be {{ fields.title }} but I get the classic error message like so:

The website encountered an unexpected error. Please try again later.

I then tried {{ field.title }}, {{ title }} and {{ content.title }} but nothing prints.

9

After eating dinner, washing the dishes, playing around with it and reading the comments in views-view-fields.html.twig located in core\modules\views\templates

/**
 * @file
 * Theme override to display all the fields in a row.
 *
 * Available variables:
 * - view: The view in use.
 * - fields: A list of fields, each one contains:
 *   - content: The output of the field.
 *   - raw: The raw data for the field, if it exists. This is NOT output safe.
 *   - class: The safe class ID to use.
 *   - handler: The Views field handler controlling this field.
 *   - inline: Whether or not the field should be inline.
 *   - wrapper_element: An HTML element for a wrapper.
 *   - wrapper_attributes: List of attributes for wrapper element.
 *   - separator: An optional separator that may appear before a field.
 *   - label: The field's label text.
 *   - label_element: An HTML element for a label wrapper.
 *   - label_attributes: List of attributes for label wrapper.
 *   - label_suffix: Colon after the label.
 *   - element_type: An HTML element for the field content.
 *   - element_attributes: List of attributes for HTML element for field content.
 *   - has_label_colon: A boolean indicating whether to display a colon after
 *     the label.
 *   - element_type: An HTML element for the field content.
 *   - element_attributes: List of attributes for HTML element for field content.
 * - row: The raw result from the query, with all data it fetched.
 *
 * @see template_preprocess_views_view_fields()
 */

{{ fields.title.content }} worked for me. In hindsight it's very similar to the D7 code, duh, lol.

For other fields:

{{ fields.machine_name.content }} What's the machine name? You can find them under the Machine Name column in the Manage Fields of your content type or block.

0

I have figured a way using kint.

Inside your views-view-unformatted.html.twig use the following code to display your individual fields:

{% for row in rows %}

{{ row.content['#view'].style_plugin.render_tokens[ loop.index0 ]['{{ YOUR_FIELD_NAME }}'] }}

{% endfor %}
  • Nice, thx for sharing :-) – No Sssweat Mar 31 '17 at 2:53
0

An easier way to do this is to install HTML Field Formatter

Then, go to your field display settings and pick the new HTML formatter. It works with Views in D8, too

  • 1
    Sorry but your answer has nothing to do with the question. The question asks how to print a field in twig (template file). Why would anyone install an HTML field formatter when you can already use HTML on Twig (template file). – No Sssweat Aug 2 '17 at 6:52

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.