Created
April 26, 2017 12:51
-
-
Save mootari/487b75692c4adca668ec54f4c39852c4 to your computer and use it in GitHub Desktop.
Drupal 7: Get base query of a view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public static function getBaseQuery($view_name, $display_id, $clear_fields = false) { | |
$view = views_get_view($view_name)->copy(); | |
// Set dummy input to avoid fetching input from $_GET. | |
$view->exposed_input = array('' => ''); | |
$view->set_arguments(array()); | |
$view->set_display($display_id); | |
// Make sure the exposed form is not rendered, otherwise we end up in a | |
// recursion. | |
$display = $view->display_handler; | |
$display->has_exposed = false; | |
foreach($display->get_handlers('filter') as $handler_id => $handler) { | |
if ($handler->can_expose() && $handler->is_exposed()) { | |
unset($display->handlers['filter'][$handler_id]); | |
} | |
} | |
$view->pre_execute(); | |
$view->build($display_id); | |
/* @var $query \SelectQuery */ | |
$query = $view->build_info['query']; | |
$query->range(); | |
if($clear_fields) { | |
$fields = &$query->getFields(); | |
$order = &$query->getOrderBy(); | |
$fields = array(); | |
$order = array(); | |
} | |
return $query; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment