Drupal - Hide a view if less than N results

You could in a template preprocess function easily detect the number of results (rows) that a view has and set the output to an empty string if that is the case.

To get this to work, you might need to do a bit of work in template, as Views always adds some wrapping HTML that you probably don't want if the view is empty.

I would probably be easiest to do in the template_preprocess_views_view() preprocess function. You can consult the views interface to get hints about templates.


Based on the hint googletorp gave, my simple solution is to put this into my template.php:

function MY_THEME_NAME_preprocess_views_view(&$vars) {
  if ($vars['display_id'] == 'MY_DISPLAY_ID' && count($vars['view']->result) < 2) {
    $vars['view']->result = NULL;
  }
}

In this case I am hiding the view if it has less than two results.

Thanks a lot!

Tags:

Views

7