Hi guys,

In a project I use several views as the data sources of a pagination the user can use on frontend to click through the results of a view (for example: to get from one node directly to the next node of the view result or to the first/ last node of the result). Therefore the views data is stored in the session and re-used later. For example: if the user uses a view to filter and sort some nodes the whole information regarding views name, display, exposed filter values and sorting gets stored in session so that these information can be used again later on to build the pagination.

What works just fine is the storing of the data which is is extracted from the view object.

$pager_data = new stdClass();
$pager_data->currentViewsName = $view->name;
$pager_data->currentViewsDisplay = $view->current_display;
$pager_data->currentFiltersRaw = $view->exposed_raw_input;
$_SESSION['pager'] = $pager_data;

Later on I need to rebuild the view from time to time. I store the results in session as well to avoid database queries but as the project has many thousands of nodes we can not save all possible nodes but we have to keep the views paging. Let's say the user clicks on the pagination on the "to the last node" link I need to rebuild the view with the last possible page. $this is here the pager-object

$this = $_SESSION['pager'];
$view = views_get_view($this->currentViewsName);
$view->set_current_page(4);
$view->exposed_raw_input = $this->currentFiltersRaw;
$view->execute();

Until know everything works fine. The problem is the sorting of the view. It seems that the table display type overrides the view somehow as I do not get the sorting information from the view object in a manner I can use later. The only way to get the sorting order seems to be the following:

$pager_data->currentSorting = $view->query->orderby;

But what ever I try - I am not able to re-use this information to rebuild the view respecting the order:

$view->add_item($this->currentViewsDisplay, 'sort', 'users', 'uid', array('order' => 'ASC'));

-> does not work, it gets overwritten when the view is executed.

pre_execute();
if ($this->currentSorting) {
     $view->query->orderby = $this->currentSorting;
}

-> doesn't work as well - it seems that with $view->execute() this query gets overwritten again.

Is there any way to sort a table view programmatically when using views_get_view() ?

Best,
Tobias

Comments

tobiberlin created an issue. See original summary.

tobiberlin’s picture

I know did it in another way: when I start building the view with views_get_view() I add the pager to the $view object:

$this = $_SESSION['pager'];
$view = views_get_view($this->currentViewsName);
$view->set_current_page(4);
$view->exposed_raw_input = $this->currentFiltersRaw;
$view->myPager = $this;
$view->execute();

Then later I use hook_views_query_alter() to alter the query before the view is executed:


function pagermodule_views_query_alter(&$view, &$query) {
  if ( isset($view->myPager)) {
    $pager = $view->myPager;
    $query->orderby = $pager->getCurrentSorting();
  }
}

Seems to be not so smart to me but as I found no other way to apply the sorting again to the view at least this is working.

Best,
Tobias

MustangGB’s picture

Status: Active » Closed (outdated)

Closing this as outdated to tidy up a bit around here. If you're still having problems with the latest release please create a new issue.