I have a view that pulls in fields from a content type. I am trying to print only the raw value of one of the fields. Since there doesn't appear to be any clear documentation on how to do this anywhere, I am forced to ask here.
I have a views-view-field--field-name.html.twig template file. I have tried multiple things to get the field to render, and aside from using {{ output -}} nothing works.
Things I have tried.
{{ field.field_name.value }}
{{ field.field_name.value|raw }}
{{ field.field_name.content }}
{{ field.field_name.content|raw }}
{% set value = row[field.field_name] %}
{{ value }}
All of which do absolutely nothing.
Can somebody please give me an answer, since there isn't a clear one documented anywhere?
Comments
It seems that the following
It seems that the following solution worked for us:
{% set val = field.field_name[0]|raw|render|render %}{{ val }}
You can also try:
You can also try:
{{ view.field.field_name.original_value }}
Worked great for me. Thanks
Worked great for me in theme override for file: core/modules/views/templates/views-view-unformatted.html.twig
Thanks for the tip!
This field can be a trap in
This field can be a trap in normal usage. That field actually contains the last value rendered by the handler which during the building rendered rows it should contain the original value _but_ in a twig template or post processing or other cases where the rows are already populated it will only contain the last value.
What would be the solution
What would be the solution that's _not_ a trap? Replacing "original_value" with "value" or "content" doesn't work. As you mention, "original_value" doesn't include any rewriting for the field configured in views. How can we call a views field in twig and keep the rewriting?
This worked for me. Thanks!
This worked for me.
Thanks!
Hi @Irous,
Hi @Irous,
This worked perfectly on my views template file- views-view-unformatted--field_name.html.twig.
Worked for me. Thanks
Thanks this worked for me as well.
Thanks. Worked for me on
Thanks. Worked for me on Drupal 10.
I know this is an old thread but...
This worked for me: {{ view.field.field_name.original_value }}
I'm not sure what the "original_value" is and I'm not familiar with the array that views spits out for Twig to interpret but it worked. Usually, in non-view Twig templates like node.html.twig, the thing that is the original value is captured with the ".0" at the end. I tried that but it didn't do anything in my view's Twig template. So I stuck with the ".original_value" and it worked just fine.
I'm sure this is in the documentation somewhere but it's not easy to find for us non-programmer designer/front-end dev types.
For more broad context, the template that I created to override the main views.view.html.twig template ended up being called "views-view--block_1.html.twig" ("block_1" is the machine name Drupal gave my view and note, I had to use the Drupal VIEW machine name, not the view block's machine name... this confused me for a few hours).
Inside of my views-view--block_1.html.twig, I removed a bunch of the default stuff and basically, just have this (in pseudo markup for the sake of simplicity here):
Of course, there is the default markup above my {% if rows %} which I didn't include in the above example.
Thanks @lrous for the easy to copy/paste syntax.
*Edit: After implementing this on my dev site, I see the trap that @neclimdul was talking about... I can get it to render the value that I'm expecting it to render but on subsequent pages that have this same view block, the values aren't being rendered. It seems like it might be related to the trap. So I'm back to Googling, unfortunately.
Views Fail (or maybe I'm just still a n00b)
Just to follow up here for the sake of brevity...
I ended up abandoning my attempt at creating a views twig template file ("views-view--block_1.html.twig" above) in favor or using the views interface and overwriting the output using a Global: Custom text field. I don't like relying on the interface for stuff like this but I couldn't figure out another way even after hours and hours of Googling. I like having my template related stuff neatly contained in tpl files but I think I'll just have to make an exception here.
Maybe it's just too many layers deep within Drupal's data to create a template without some fancy PHP functions.
I spent far too long trying
I spent far too long trying to get this to work. I'm more of a designer than a developer but it seems to me this should be easier, or actually documented.
Anyway, I couldn't get {{ view.field.body.original_value }} to work. It would just duplicate the value from the last rendered row.
The only thing I found that worked is:
{{ row.content['#row']._entity.body.value | raw }}
The clean way would be to use
The clean way would be to use preprocess hook to get what you need and pass a variable to the template and use it there.