(Spin-off of Remove/Hide the block if the View is empty?. I find the other question a bit confusing, and want to reduce it to the essential question.)
When rendering a view programmatically, and there are no results, there is still some HTML returned from $view->render().
This can be desirable in the following cases:
- There is a “No results behavior”.
- There are exposed filters, which need to be shown independent of the view result.
- The view is using AJAX, so it could happen that new content appears dynamically.
- Perhaps other reasons?
I want to write generic code that renders a view, but completely hides it if there are no results, and no other reason to show it.
So far I have this:
$view = views_get_view($view_name); if (NULL === $view) {   return []; } $success = $view->set_display($display_id); if (FALSE === $success) {   return []; } $view->set_arguments($args); // See https://www.drupal.org/node/525592#comment-1833824 $view->override_path = $_GET['q'];  $view->pre_execute(); $view->execute();  if (empty($view->result) && empty($view->empty)) {   // @todo Also check for exposed filters!   return []; }  $markup = $view->render();  $view->post_execute();  if (FALSE === $markup) {   return []; } return ['#markup' => $markup]; So the question is: Which other checks should I add when looking if the view is empty? (the place with the @todo).
How do I check if there are exposed filters? Which other criteria are relevant?



