In hook_views_pre_render(&$view) I have a simple code:

foreach ($view -> result as $key => &$result) {
    if('some condition') {
        unset($view -> result[$key]);
    if('some condition') {
        unset($view -> result[$key]);

Above works OK. it unsets result row of the view. Now, in my view I am displaying 2 results per page. Currently have it outputs 10 node which means there are 5 pages.

The problem is, as you might suspect that if, for example, 2 unset results happened to be on page number, say 2, of the view, this page is still displayed in the pager. So what I have now is 2 results on page number 1, no results on page number 2 and 2 results on page number 3.

Similarly, if only 1 result is unset, I have 2 results on page number 1, 1 result on page number 2 and 2 results on page number 3.

is there a way of letting the view know that it needs to update it's structure? There is a query_alter possible solution but I would like to avoid it if possible.

I do realise that this might not be the best way to unset results and I am open to suggestions, however i would like to keep the code in my custom module (not being fore to alter template.php).

  • have you tried modifying your results in the hook_views_post_execute() phase instead? api.drupal.org/api/views/views.api.php/function/… – Jimajamma Nov 26 '13 at 20:25
  • @Jimajamma, unfortunately it has the same result like pre_render(). – sylwester Nov 26 '13 at 22:05
  • it was a shot; but this shows that the sql for creating the page with the various limits, offsets, ranges etc is running before we get to this point. thus it seems you will have to do a query alter. – Jimajamma Nov 26 '13 at 22:23
  • 1
    Hmm... check out answer #4 here... drupal.org/node/953072 – Jimajamma Nov 26 '13 at 22:31
  • I looked at this yesterday and it didn't work in my pre_render(). After I changed to post_execute() and trying it again today it worked great Thanks a lot for pointing me back to that! Do you want to post it as an answer so I can accept it? – sylwester Nov 27 '13 at 11:04

You may want to look at doing this in Views' hook_views_post_execute():

This hook is called right after the execute process. The query has been executed, but the pre_render() phase has not yet happened for handlers.

as the paging happens in the handler phase, and hook_views_pre_render()

This hook is called right before the render process. The query has been executed, and the pre_render() phase has already happened for handlers, so all data should be available.

All about the various Views hooks is available here:


Also, some interesting conversation about this process and potential alternate suggestions is available here in a drupal.org issue queue "update pager count after chaning results in hook_views_pre_render" as well.

|improve this answer|||||

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.