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.

  • 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. – Alfred Armstrong Nov 5 '15 at 11:50
  • Probably. How can i do "pick all the properties named 'total_items' and set it X"? – Sasha Grievus Nov 5 '15 at 12:00

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') {

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.

  • You mean even the removing of items to the view must be done in pre-build? – Sasha Grievus Nov 5 '15 at 12:04
  • You wouldn't remove anything with this method, unwanted results would simply never be loaded from the database – Clive Nov 5 '15 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. – Sasha Grievus Nov 5 '15 at 12:13
  • 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 '15 at 12:41
  • yep! Maybe i have only to change all the 'total_items' properties but i don't really know how to parse the object :) – Sasha Grievus Nov 5 '15 at 12:45

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.