2

I have a price field that I need to hide if the value is 0. As I want to use :- as a suffix I turn on "Show prefix and suffix". Unfortunately this makes the field visible again. In my different views I have managed to hide the field anyway with this snippet in hook_views_pre_render(&$view):

foreach($view->result as $val){
    if(isset($val->field_field_pris[0]['raw']['value'])){
        if($val->field_field_pris[0]['raw']['value'] == '0'){
            $val->field_field_pris[0]['rendered']['#access'] = FALSE;
        }
    }
}

But in my search result page (I use the Search API module), this does not work. For some reason I have no access to the fields in the variable $view->result. So how can I hide a field in the search result view if the field value is zero, and still use the prefix & suffix?

EDITED: Ok, so I created a field template file as suggested and replaced the <?php print $output ?> line with this code:

<?php
if($output) {
  $pris = $row->_field_data['field_pris_tillbehor']['entity']->field_pris_tillbehor['und'][0]['value'];
  dsm($pris);
  if($pris == 0){
    dsm($row);
    hide($row->_field_data['field_pris_tillbehor']['entity']->field_pris_tillbehor);
    print $output;
  }
}?>

... but even though the zero value is confirmed by the dsm statement, the value still appears in the search page view.

  • Have you tried hiding it using the views UI? – No Sssweat Jan 7 '17 at 12:42
  • 2
    Oh I see, yeah checking Count the number 0 as empty in No Results Behavior, does not work. Try creating a fields template file for your view, then do the if statement there. – No Sssweat Jan 7 '17 at 12:51
  • That sounds as a solution, but I didn't succeed doing it in a way that works. Can you see what I am doing wrong in my edited question above? – TBJ Jan 7 '17 at 14:00
  • 1
    Finally got it. Se my final edit above. – TBJ Jan 7 '17 at 18:08
  • There is an issue on drupal.org relating to this: drupal.org/project/views/issues/1239522 – rooby May 8 '18 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.