2

I had to remove manually some rows from a View programmatically for a lot of good reasons, too long to explain. I did it removing item in the pre-render hook from the array

view -> result

It works fine, but the total number of items contained in the view is not changed. How can i change it programmatically? I tried changing

view -> total_rows 

but it seems not to be enough. I saw a lot of total_items elements in the object view, and i think changing them would work, but there are 60 items like that, each contained in objects contained in arrays that are contained in objects and so on and i am not really sure how to change every element with that name, parsing the structure of the object view and changing every total_item with the number X instead of the number Y.

2
  • Just as an aside, because objects in PHP are all implicitly addressed by reference I doubt there are actually 60 such objects: they are probably all the same few objects - the view and its displays. Nov 5, 2015 at 11:50
  • Probably. How can i do "pick all the properties named 'total_items' and set it X"? Nov 5, 2015 at 12:00

1 Answer 1

2

You're probably looking for the view::set_items_per_page() method, e.g.

/**
 * Implements hook_views_pre_build 
 **/
function MYMODULE_views_pre_build(&$view) {
  if ($view->name == 'foo') {
    $view->set_items_per_page(10);
  }
}

That method updates both the instance var and the limit on the internal query that Views will eventually run.

Note that you need to make this change before the query is executed (i.e. in a pre build, not pre render, hook), or you'll get inconsistencies with pagers etc.

7
  • You mean even the removing of items to the view must be done in pre-build? Nov 5, 2015 at 12:04
  • 1
    You wouldn't remove anything with this method, unwanted results would simply never be loaded from the database
    – Clive
    Nov 5, 2015 at 12:05
  • I removed rows from the view at pre-render time removing element from the object view fulfilling some field condition, let's say 'the body of the node is in english'. At pre-build time (that is before pre render) i do not have in the object view the data for checking the condition 'the body is in english' and there is no row still to remove. So, i can remove rows at pre render but then i have a huge inconsistencies with pager and that's why i want to change the total item number. Nov 5, 2015 at 12:13
  • 1
    As the answer mentions, you need to perform this change before the query is run. In pre render is too late, because the database query has been run, so the LIMIT can't be adjusted. If Views isn't giving you the information you need at that point, you'll need to source it from elsewhere, or, if you can represent what you need to get via SQL, alter the query to make the changes
    – Clive
    Nov 5, 2015 at 12:41
  • 1
    That\s not going to work either (if you have a pager anyway). Say you have 10 items returned in the query, and you hide 5 of them and only show 5. On your next page, you're not going to see the 5 that you've hidden previously, you're going to see 5 shown and 5 hidden from the next set of 10 records. So you'll effectively lost the ability to view half of the available records. Make sense?
    – Clive
    Nov 5, 2015 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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