While working on views_calc (http://drupal.org/project/views_calc) i noticed some strange behaviour.
In pre_render, views_calc clones the view and executes some special query to execute aggregate functions (SUM, COUNT, ...). During this aggregation, views_calc removes paging on the cloned table and calc the resulting aggregation data.
This execution of the cloned view also affects the pager of the original view - it suddenly disappeary.
Deactivating the execution of the subquery leads to an active pager again.
Find the snippet that causes the issue:
function pre_render($results) {
parent::pre_render($results);
// If there are no calc fields, do nothing.
if (!$calc_fields = $this->get_calc_fields()) {
return;
}
...
// Add grand totals to the results.
foreach ($calc_fields as $calc => $field) {
if ($summary_view = views_get_view($this->view->name)) { // <<-- views does a custom semi-deep copy here!
//$summary_view->set_display($this->view->current_display);
$summary_view->set_arguments($this->view->args);
$summary_view->pager['items_per_page'] = 0;
$summary_view->views_calc_calculation = $calc;
$summary_view->views_calc_ids = array();
$summary_view->views_calc_sub_total = FALSE;
$summary_view->is_cacheable = FALSE;
$summary_view->preview(); // <<-- this execution affects the pager of the original query (in $this)
$this->view->totals[] = array_shift($summary_view->result);
}
}
It seems to me like views doesn't clone enough in views_get_view (finally $view->clone_view()).
What part do we miss here?
Comments
Comment #1
miro_dietikerI already tried cloning in different ways like:
and
Whereas summary_view2 was the return from views_get_view().
Comment #2
miro_dietikerAlso tried to unset the pager in pre_render of all components.
In the loop
And outside of the loop
Also tried looking at the global pager part:
Nothing helped.
Comment #3
miro_dietikerHere we go:
If the subquery has some pager enabled, the global drupal pagers are rewritten.
(see includes/views.inc @750)
However, disabling the pager in the way views_calc did on $view, won't work since views pre_exec function on the display handler will set it back to the display pager settings. Thus instead we need to switch to the display level and set the pager option there.
Switching over to Views Calc since this issue originally broke Views Calc. Providing patch that solved the issue after some cleanup!
Comment #4
miro_dietikerCommitting attached patch.
Setting the display is essential. Only after that we can disable the pager with effect on the display level. This has to be done per page and total.
Now the global (page) pager is no more accidentally affected with the cloned sub views for aggregation query.