5

I use Views 7.x-3.8.
I have view "taxonomy_term". It has Paged output, full pager. Number of pages (the total number of pages) is set to 100, Items per page = 10. Now I want to show all pages for users with proper permission.

I tried to set pager option 'total_pages' to empty string in hook_views_pre_execute():

function MYMODULE_views_pre_execute(&$view) {
  if ($view->name == 'taxonomy_term') {
    if (user_access('view all pages')) {
      $view->query->pager->options['total_pages'] = '';
    }
  }
}

It actually works with pager: users with permission 'view all pages' can view page #101, #102 and so on, but nodes on these pages (more than #100) are the same as on page #100. It means that query has not changed, and query limit is still 1000 (10 items x 100 pages). I tried

$view->query->set_limit(0);

in hook_views_pre_execute() and

$view->query->set_limit(0);
$query->set_limit(0);

in hook_views_query_alter(), but it's not working.

I also tried to set $view->query->offset and $view->offset manually:

$view->query->offset = $view->offset = $view->query->pager->current_page * $view->query->limit;

with no effect.

What am I missing? How to change number of view' total pages AND query results programmatically?

3

I did it:

function MYMODULE_views_pre_execute(&$view) {
  if ($view->name == 'taxonomy_term') {
    if (user_access('view all pages')) {
      $view->query->pager->options['total_pages'] = NULL;
      $query = $view->build_info['query'];
      $offset = $view->query->pager->current_page * $view->query->limit;
      $query->range($offset, $view->query->limit);
    }
  }
}
  • I didn't need to do this exact thing, but seeing how you've solved your problem pointed me in the right direction for mine. Thank you! – Scott Joudry Jun 1 '18 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.