Hi all,

I've always used the following helper function to check if a view has results:

function _helper_view_has_results($view, $display, $args) {
  $view = views_get_view($view);
  $view->set_display($display);
  $view->args = $args;
  $output = $view->preview();
  if ($view->result) {
    return TRUE; // ok, there are results.
  }
  return FALSE; // no results.
}

It works correctly with views that haven't any exposed filters, but doesn't work with views that have exposed filters.

Can anybody help me to resolve please?

Thank you very much.

CommentFileSizeAuthor
#3 Browser error.jpg96.46 KBMXT
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

MXT’s picture

7 months later... No answer on this.

Please, help me!

dawehner’s picture

Keep calm, there are so many issues, that you simply can't answer them all.

Do you have already the full view object available in your context of the function?
Alternative you could try to use views_get_current_view() and based on that check via $view->result.

MXT’s picture

FileSize
96.46 KB

Hi dawehner and thank you for your answer.

I'll try to explain better the issue:

I'm trying to hide a View TAB menu if that view returns no results. This is quite complicated due to http://drupal.org/node/1217394.

But finally I've found a good workaround, as described here: http://drupal.org/node/1485188

The workaround works very well, view menu tab is shown only if there are results. This works for all Views, except for that ones that have exposed filters: in this case, if you click on the tab (that is shown only if there are results) the website breaks down completely: I receive no errors/warning, no blank page of death, only a browser error (see screenshot in attachment).

If, for the same view, I temporarily disable my exposed filters, all works fine.

In the above function, the issue seems triggered by this line: $output = $view->preview();:

function _helper_view_has_results($view_name, $display, $args) {
  $view = views_get_view($view_name); // no problem here, $view object is correctly retrieved
  $view->set_display($display); // no problem here
  $view->args = $args; // no problem here
  $output = $view->preview(); // THIS LINE PRODUCES THE ISSUE ONLY FOR VIEWS WITH EXPOSED FILTERS
  if ($view->result) {
    return TRUE; // ok, there are results.
  }
  return FALSE; // no results.
}

thank you very much for helping me

dawehner’s picture

To be honest, running a view to determine whether a menu is visible seems to be broken by design.

You might have more luck with just using views_get_view_result().

MXT’s picture

Ok, I've changed the function like this:

function _helper_view_has_results($view_name, $display, $args) {
  $output = views_get_view_result($view_name, $display);
  if ( !empty($output) ) {
    return TRUE;  // ok, there are results.
  }
  return FALSE; // no results.
}

But the issue is still present.

Can you please give me your opinion about the reason why the error occurs only with views with exposed filters?

Thank you

dawehner’s picture

I don't know yet the actual problem and would have to reproduce it, sorry.

MustangGB’s picture

Issue summary: View changes
Status: Active » Closed (outdated)