Hi

Spent a couple of hours trying to figure out something I assumed would be really easy but I'm not getting it

I have over ridden my views-view-fields.html.twig file and that works fine

I render my fields using {{ fields.uid.content }} and that also works fine

What doesn't is if I try and run an if statement over it. 

fields.uid.0 returns nothing

fields.uid.value also returns nothing

fields.uid causes an error

so how do I do something like 

{% if fields.uid.content == 'John' %} ...

I need to do this in the fields twig template, not the field twig template as im adding some additional html in a separate place in the template based on the result

Many Thanks

Comments

wombatbuddy’s picture

so how do I do something like 

{% if fields.uid.content == 'John' %} ...

What problems do you have with this expression? 

gibbo1715’s picture

It displays on the screen fine but if I try and do an if statement against it it doesn't work, I assume it is returning an array or similar and not just the raw value. So when I do {% if fields.uid.content == 'John' %} do x else do y, even when the content is John it still does y and doesn't recognise the content is John

wombatbuddy’s picture

You can explore it with kint() yourself.
But I gues that the reason may be that it output additional debug info.
You can try to remove it using 'striptags' filter, for instance, like this:

{% if (fields.uid.content|striptags) == 'John' %}
gibbo1715’s picture

That didn't work for me though unfortunately but did get me reading the right posts and based on what I read I went a different route as follows:

I just changed the output in my theme.module file and used the following (Added below to help others struggling with the same thing)

/**
 * Implements hook_preprocess_form_element().
 */
function YOURTHEME_preprocess_views_view_field(&$variables) {

  // Do something based on the name of the field
  if ($variables['field']->field == 'FIELD_NAME') {

  // Modify the output to just pass the value with no markup
  $variables['output'] = $variables['field']->getValue($variables['row']);

  }
beg76’s picture

Hi,

i run into same issues. I did copy your function into my_theme.theme, but i do get a syntax error. Do you have an idea whats wrong? Thanks a lot for helping!

/**
* Implements hook_preprocess_form_element().
*/
function my_theme_preprocess_views_view_field(&$variables) {

if ($variables['field']->field == 'field_typ') {
$variables['output'] = $variables['field']->getValue($variables['row']);

}