Skip to content

Instantly share code, notes, and snippets.

@augustyip
Created April 9, 2014 09:10
Show Gist options
  • Save augustyip/10245177 to your computer and use it in GitHub Desktop.
Save augustyip/10245177 to your computer and use it in GitHub Desktop.
Drupal: Displays the output of a view, assumed to be a block display.
<?php
/**
* Displays the output of a view, assumed to be a block display.
*
* @param $view_name
* Machine name of the view.
* @param $display_name
* Machine name of the display within the view.
* @param $args
* (optional) Array of arguments for the view display.
*
* @return
* String containing the view output. If the view was empty, an empty string.
*/
function module_display_view($view_name, $display_name, $args = array()) {
$output = '';
// Load the view.
$view = views_get_view($view_name);
if ($view) {
// Override the URL so that exposed filters will work on blocks even without
// AJAX.
$view->override_url = current_path();
$output = $view->preview($display_name, $args);
}
// Check for no content.
if (!strlen(trim(strip_tags($output)))) {
$output = '';
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment