3

I've created a display called 'stats' for some views I've created - its cached and its generic and its quick and I use it for things like this

$result = views_get_view_result($view_name,'stats'); 
$count = count($result);
if ($count) $variables['title'] .= ' ('.$count.')';

However, not all views have this display. How can I check, in php, if a given view has a display called 'stats' ?

2 Answers 2

5

Off the cuff, this should work:

$view = views_get_view($view_name);
if ($view && $view->access('stats')) {
  // it has a 'stats' display
  $view->set_display('stats');
  $view->pre_execute();
  $view->execute();
  return $view->result; // or do whatever with it...
} 

but realize at this point you're halfway through the 'heavy lifting' views does, so you should probably use $view inside that if instead of then calling views_get_view_result()

ADDITION: I added what I think the correct $view incantations should be for getting just the results above :)

0

First make a views_get_view($view_name); it will give you the $view object, which I'm sure will tell you which display it has. Then you'll be able to if().

Your Answer

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

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