Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#3 | Browser error.jpg | 96.46 KB | MXT |
Comments
Comment #1
MXT7 months later... No answer on this.
Please, help me!
Comment #2
dawehnerKeep 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.
Comment #3
MXTHi 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();:
thank you very much for helping me
Comment #4
dawehnerTo 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().
Comment #5
MXTOk, I've changed the function like this:
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
Comment #6
dawehnerI don't know yet the actual problem and would have to reproduce it, sorry.
Comment #7
MustangGB CreditAttribution: MustangGB commented