Say for instance, you have a view that displays when viewing a certain node type, but you want the pager settings to be different for each individual node. I was thinking of making a CCK field like "pager setting" and then give it an integer option of x-y. But I don't know if it's possible to somehow dynamically plug that field into the Views' settings. Or is there another way to do this maybe?
5 Answers
The views hook that you would want to use is hook_views_pre_build
which is called before the query is built. Now this is assuming you have some basic module development experience and that you are familiar with the views api.
You should be able to do :
/*
* Implementation of hook_views_pre_build().
*/
function hook_views_pre_build(&$view) {
// Make sure this is only for the specific view you want to modified
if ($view->name == "foo_bar") {
// Get the x-y value from where you're storing it (in your example the node object).
$pager_count = get_count_for_this_node();
// Lets also make sure that this is a number so we won't destroy our view.
if (is_numeric($pager_count)) {
// Now lets set the pager item to what ever out count is.
$view->pager['items_per_page'] = $pager_count;
}
}
}
Above we're using a views hook that's called before the view query is built that way the pager and everything else will reflect the change.
Word of caution: views hooks should only be used if you understand whats going on. The above code is written for views-2.x.
Hope this helps.
-
Awesome, thank you. I don't have any module development experience yet, but I'm starting out on it for a couple of things I want to accomplish. I do have a working grasp of PHP, so I can follow along what's happening in that code chunk you pasted above. I think this well help me a lot. Thank you.– JayCommented Mar 30, 2011 at 6:46
-
1For Views 3.x the relevant code should be changed to
$view->items_per_page = $pager_count;
Commented Jul 29, 2014 at 8:21 -
is it possible to set offset of each page dynamically too? Commented Feb 20, 2017 at 8:19
For Drupal 7, Only should write the following:
$view->items_per_page = $pager_count;
In the example:
/**
* Implements hook_views_pre_build().
*/
function module_name_views_pre_build(&$view) {
if ($view->name == "foo_bar" && $view->current_display == 'foo_display') {
$pager_count = get_count_for_this_node();
if (is_numeric($pager_count)) {
$view->items_per_page = $pager_count;
}
}
}
I use code example by @ericduran.
You should to use views preprocess function
/*
* Implementation of hook_views_pre_render().
*/
function MYMODULE_views_pre_render(&$view){
// $view->name
// $view->current_display
// ...
// look for other variables in $view object
}
-
"preprocess" is for theming and pre_render is too late (he query was allready run)- the pre_build hook is much better.– mojzisCommented Jan 6, 2013 at 11:32
To update views result and pager in hook_views_pre_render, you can do following:
<?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;
}
}
This should work!! ;)
-
Excellent! I wanted to limit the number of items on a given page depended on the items returned by the query. Will check out this method ASAP.– JensCommented May 19, 2013 at 13:22
-
It does not exactly work. The rows you discharge does not turn up on the next page. So you end up with missing content. Needs more investigation.– JensCommented May 19, 2013 at 14:44
@tanmayk 's code did the trick for me. In hook_views_pre_render added just these 2 lines of code
$view->query->pager->total_items = $nr;
$view->query->pager->update_page_info();
But I didn't need to add the results to the view.