Dear all,

I have a question regarding the pager count problem that I faced when I tried to take away some rows from the views through the function "hook_views_pre_render".

So my scenario right now is:
At first there should be 152 records, but after I did some changes in hook_views_pre_render, the final result is 148 now.
The pager I've set is 30 per page, so in the original setting (152) there should be 6 pages, and after doing changes in hook_views_pre_render, it should only have 5 pages (148 records).

But I found no way to change the total count of the records for the pagination thing. Any idea?

Thanks a lot!
Gordon

Comments

dawehner’s picture

There is currently no hook after the count query and setting up the pager variables

          if (!empty($this->pager['use_pager'])) {
            // dump information about what we already know into the globals
            global $pager_page_array, $pager_total, $pager_total_items;
            // total rows in query
            $pager_total_items[$this->pager['element']] = $this->total_rows;
            // total pages
            $pager_total[$this->pager['element']] = ceil($pager_total_items[$this->pager['element']] / $this->pager['items_per_page']);

            // What page was requested:
            $pager_page_array = isset($_GET['page']) ? explode(',', $_GET['page']) : array();

            // If the requested page was within range. $this->pager['current_page']
            // defaults to 0 so we don't need to set it in an out-of-range condition.
            if (!empty($pager_page_array[$this->pager['element']])) {
              $page = intval($pager_page_array[$this->pager['element']]);
              if ($page > 0 && $page < $pager_total[$this->pager['element']]) {
                $this->pager['current_page'] = $page;
              }
            }
            $pager_page_array[$this->pager['element']] = $this->pager['current_page'];

So i fear you have to set up it for yourself. This is code out of includes/view.inc::execute

gortang’s picture

Status: Active » Closed (fixed)

Thanks so much dereine!

I solved the problem by adding these lines to my module's prerender code:

            global $pager_page_array, $pager_total, $pager_total_items;
            // total rows in query
            $pager_total_items[$this->pager['element']] = $this->total_rows;
            // total pages
            $pager_total[$this->pager['element']] = ceil($pager_total_items[$this->pager['element']] / $this->pager['items_per_page']);

Thanks a lot!
Cheers,
Gordon

maria_zk’s picture

I have exactly the same problem!
Could you please share some more info on how you people solved this? Maybe some code?
This does nothing in my views pre render case:

      global $pager_page_array, $pager_total, $pager_total_items;
            // total rows in query
            $pager_total_items[$view->pager['element']] = $view->total_rows  - $counter;
            // total pages
            $pager_total[$view->pager['element']] = ceil($pager_total_items[$view->pager['element']] / $view->pager['items_per_page']);

The counter here counts the removed items like

...
          $counter++;
          unset($view->result[$key]);
...

So in the end, the $pager_total_items gets the correct sum of results...

Help, please?

avr’s picture

I ran into this issue. At least in D7 and in my case, I didn't need to mess with the global $pager_XXXX arrays.

Instead, after making your changes to the number of rows, just update the total_items and then use the update_page_info() function.

So, in the end you have:

Code that adjusts the results of the view

$view->query->pager->total_items = count($resultNoDoubleNodes); // reset the count of items
$view->query->pager->update_page_info(); // update the pager object witht the correct number of items

$view->result = $resultNoDoubleNodes; // replace the old results with the results without duplicates
JMM75’s picture

Thank you so much! I've been searching for the problem now for hours. I was missing the call to update_page_info() in my hook_views_pre_render.

christiemade’s picture

#4 was exactly what I needed to get this working with D7. Thanks for passing this on!

christiemade’s picture

#4 was exactly what I needed to get this working with D7. Thanks for passing this on!

christiemade’s picture

#4 was exactly what I needed to get this working with D7. Thanks for passing this on!

Jason Dean’s picture

Issue summary: View changes

I don't understand how the solution in #4 can work, either using hook_views_pre_render() or hook_views_post_execute().

For example, let's say my view uses a pager with 10 results per page. Using either hook, I can modify the contents of the $view->result array (remove some unwanted results). But $view->result only contains 10 results, for the page that I am on.

So using the code example above:

$view->query->pager->total_items = ???; // reset the count of items
$view->query->pager->update_page_info(); // update the pager object witht the correct number of items

What do I set total_items to? These hooks only give me 10 results at a time to modify.

Thanks!