Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By rameshbabu.g on
I am using views_pre_render to alter view result. It is working fine but pagination is not working as per updated result.
<?php
/**
* Implementation of hook_views_pre_render().
*/
function MODULENAME_views_pre_render(&$view) {
if ($view->name == 'my_view' && $view->current_display == 'my_display') {
// View result update logic.
// e.g.
// $result = array();
// foreach ($view->result as $k => $row) {
// if (whatever is your condition) {
// $result[$k] = $row;
// }
// }
// Assuming $result has data as per your logic.
// Update the pager according to result.
$view->query->pager->total_items = count($result);
$view->query->pager->update_page_info();
// Add results to view.
$view->result = $result;
}
}
?>
How can i get the correct pagination with updated result.
Comments
I have the same problem in
I have the same problem in drupal 8. Do you solved it?
Try that snippet at end of
Try that snippet at end of your function.
Thank you,
Tanzeel
D8 Method
This appears to work in Drupal 8.
Thanks; Works in Drupal 9 too
Snippet works like a charm:
Michael
Filter view with different types of content
Hi, snippet works, but I think there is a problem to make it work properly.
I explain the possible problem:
In my case I am adjusting the default content view of drupal, and suppose this is paged at 5 items per page.
So:
I am going to filter by type of content, in the example I have marked an assumption that those nodes are the ones that complete the condition.
Page 0
Item 1
Item 2 -> I want this
Item 3
Item 4
Item 5
Page 1
Item 1
Item 2
Item 3
Item 4
Item 5 -> I want this
The view returns me of 5 items, that means that I will never find out about the other items that come on the following pages.
My question is how can I know how to filter correctly taking into account all the items that may be on other pager pages?
Solved with views_query_alter hook
I have the same Problem any one has solution ??
Small error in your code
isn't very important, but as I've noted above, it makes sense to do so. What you really need before updating the page info is to fix the pager's concept of the row count:
Michael