Skip to content

Instantly share code, notes, and snippets.

@mootari
Last active August 29, 2015 14:09
Show Gist options
  • Save mootari/2717e445411eac9287b5 to your computer and use it in GitHub Desktop.
Save mootari/2717e445411eac9287b5 to your computer and use it in GitHub Desktop.
Count results for any view. #drupal #d7 #views
<?php
/**
* Returns the row count of a view.
*
* Disables user input and forces a count query.
* Code adapted from @see views_get_view_result().
*/
function EXAMPLE_get_view_count($name, $display_id = null) {
$args = func_get_args();
array_shift($args);
array_shift($args);
if(!$view = views_get_view($name)) {
return 0;
}
$view->get_total_rows = true;
// Set dummy input to avoid fetching input from $_GET.
$view->exposed_input = array('' => '');
$view->set_arguments($args);
if(!is_null($display_id)) {
$view->set_display($display_id);
}
else {
$view->init_display();
}
$view->pre_execute();
$view->execute();
return $view->total_rows;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment