6

I wonder if it's possible to hide a view (as block view) if it has less than X results.

6 Answers 6

1

It takes a bit to learn the views hooks. Do you know about view template files? If so a quick and dirty way to do it is to check how many rows there are in the highest level view template file the display output. If you have the Devel module installed you can dpm($variables) in views-view.tpl.php (you'll probably want to use the display output that is unique to your view).
I can't remember where, but somewhere in $variables is a row count; you can add the logic to the template file to not print out $rows if it does not meet your criteria.

2
  • 1
    Putting this sort of logic in the template file is discouraged. @googletorp's suggestion of using preprocess is better. Apr 14, 2011 at 23:55
  • did you miss the part I said about quick and dirty. Dirty insinuates that it is not a best practice.
    – Jepedo
    Apr 15, 2011 at 0:10
8

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!

1
  • 3
    If you use this code, first make sure that MY_DISPLAY_ID is unique site-wide (you can edit it in the view under Other - machine name). Often it's something very generic like "block" because it only has to be unique within a view.
    – Yuri
    Mar 18, 2015 at 14:34
8

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.

4
  • I don't understand this at all. If you need to do work inside a tpl.php file anyway to make sure all the wrapper HTML is removed, what's the point of modifying the template.php file also? Just seems like you're creating extra work. Feb 28, 2012 at 0:48
  • 1
    @LesterPeabody Best practice is to place logic in the preprocess and only handle the markup in the template. Makes your code more maintainable.
    – googletorp
    Feb 28, 2012 at 8:05
  • There is logic everywhere in views template files... maybe you could provide an example? For example, I simply surrounded an instance of the views-view.tpl.php file with <?php if ($rows): ?> ... <?php endif; ?>. My understanding currently is there is no way to eliminate the extraneous wrapper HTML otherwise. If the $rows` variable is already available to me in that file, why would I do extra work in template.php? Feb 28, 2012 at 15:07
  • @LesterPeabody Generic, Figuring out if something is there before rendering is common. But special logic for a specific view does not belong in a template file. It's harder to maintain, and when building complex sites that you need to maintain this becomes more obvious.
    – googletorp
    Feb 29, 2012 at 8:50
1

If the reason for this is primarily display-oriented (e.g., you are building a grid and want to force an even/odd number of items) then I suppose adding custom logic at the theme layer is certainly the most logical. The other answers have covered this I think. However, the issue that we found is that some views variables that may need to be changed (e.g., $view->result = NULL), cannot safely be altered as part of a preprocess function. It seems that views internal caching may generate some variables in such a way that some combinations of "rendered output" and "query result" cache settings can be incompatible with any of the existing solutions.

One alternative is to alter the results of the executed query itself, which offers control at a much lower level. This might also get around some of the issues with hiding wrapper markup as it simulates an "empty" results set even before the markup is built.

/**
 * Implements hook_views_post_execute().
 */
function MYMODULE_views_post_execute(&$view) {
  $view_name = 'myviewname';
  $view_display = 'mydisplay';
  // Don't show the view if there is only one item.
  if ($view->name == $view_name && $view->current_display == $view_display && count($view->result) < 2) {
    $view->result = array();
  }
}

This of course can't be implemented in a theme as it's not related to a theme or alter hook, but I think it's still worth noting for people interested in this question.

0

In some cases it is nicer for this sort of conditional to be put in the view, so that the builder understands what is happening within the edit view context, not in some other module or theme hook.

If you prefer having this logic included on the view edit page then you can do this:

  1. Add a Global Text/HTML footer field and make the text format PHP Code
  2. Set the admin name of this field to something very clear, e.g. 'Hide this view if less than 5 results', so that future site builders won't be confused (why is my view disappearing!?!?)
  3. Modify the below PHP code to suit your needs and use in the field:
<?php
$view = views_get_current_view();
if ( count($view->result) < 5 ) {
    $view->result = null;
}
?>
2
  • Hmmmm. This sounds like it would work, but adding PHP code to the database is usually not desirable when there's another way to do it. Nov 20, 2017 at 19:57
  • Agree with you @squarecandy, just offering as a possible alternative for certain cases.
    – RogerRoger
    Nov 22, 2017 at 21:21
0

You could also do this using the theme templating:

  • Copy views-view.php to your theme/templates directory
  • make a copy of that file and name it views-view--view-name--display-name.tpl.php
  • (See Advanced > Other > Theme:Information for more detail on what file names to choose and override - you can get more or less specific depending on your needs)
  • add if (count($rows) < $n): in to your code to manipulate based on the number of results as needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.