views-view-field.html.twig contains a wrong instruction to fetching output from the row

Created on 5 August 2022, almost 3 years ago
Updated 21 May 2025, 13 days ago

Problem/Motivation

The "views-view-field.html.twig" template has the following comment:
"When fetching output from the row this construct should be used:
data = row[field.field_alias]"
The above will guarantee that you'll always get the correct data, regardless
of any changes in the aliasing that might happen if the view is modified.

but this is not possible, because the row is the object of the Drupal\views\ResultRow class.

Steps to reproduce

Try to get and render a field value in the suggested way:
{{ row[field.field_alias] }}

Proposed resolution

Remove this comment or replace it with another, for example, with this one:
"You can fetch values of other fields like this:
data = view.field.field_myfield.original_value
data = row._entity.title.value
data = row._entity.field_myfield[0].value"

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component

views.module

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • Merge request !12188Create GitLab patch β†’ (Open) created by ressa
  • Pipeline finished with Success
    13 days ago
    Total: 609s
    #502773
  • πŸ‡©πŸ‡°Denmark ressa Copenhagen

    Thank you so much @wombatbuddy for raising this issue back in February 2022.

    I had been scouring the internet, for something so simple as how to insert fields in my Views Twig template file, with the replacement tokens listed under the "Replacement patterns" in a field (type "Custom text", machine name nothing) trying first with {{ title }} and then {{ row[field.field_alias] }} as recommended in the Twig template file, but it didn't work ... but then I finally found a solution here in this issue, and I am eternally grateful.

    Having a wrong solution in the Views Twig template files is not great, so perhaps this MR can get reviewed and committed before too long, and other documentation tasks handled in a follow up issue?

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Believe #34 and #35 still need to be addressed.

  • πŸ‡©πŸ‡°Denmark ressa Copenhagen

    Thanks for a fast response @smustgrave.

    About #34: I think I found the instances, are there more?

    $ grep -iR "row\[field.field_alias\]" .
    ./core/themes/stable9/templates/views/views-view-field.html.twig: * data = row[field.field_alias]
    ./core/modules/views/templates/views-view-field.html.twig: * data = row[field.field_alias]
    

    About #35: The example doesn't work for me.

    That's why I commented, that it would be great to at least share a working example, and then refine in a follow up issue. What do you think about that suggestion?

    In its current state, the non-working example is a major stumbling block, for Drupal users who want to use Views fields in a Twig template ...

Production build 0.71.5 2024