views:

112

answers:

2

Hey!

I have views 2 installed and I have created a view that is displayed in the front page.

The view displays some page links ( 1 | 2 | 3 | 4 | ... etc). I want to know if it's possible to make the view start at a random page instead of always starting at page 1.

Note: I don't want to randomize the display I really just want to randomize the page it loads.

Thanks

Possible Solution:

In the views_pre_execute hook I used this:

$view->query->pager->set_current_page([random value]);

I am not sure I can determine the number of total pages in the pager at this time but I am going to keep investigating (The $view object given in the hook has tons of properties with arrays and other objects which makes this complicated)

+2  A: 

I do not know how to do this from the Views UI, but you should be able to achieve this using one of the views module hooks, in this case probably hook_views_pre_execute. Unfortunately, the documentation for these is practically non existing, so you'd need to implement the hook in a custom module and inspect the passed in view object via the debugger (or print, var_dump, etc. statements).

You should look for $view->pager['current_page'], which you can set to a random page. Unfortunately, if I read the code correctly, the count query that determines the possible number of pages is not yet run at this point, so you'll either have to use a 'best guess', or come up with a different way to determine the proper range to select from...

NOTE: This is in no way meant as an 'authoritative' answer - just a pointer where I'd start looking, since nobody else has answered this so far. I might well be missing a more obvious/easy solution :/

Henrik Opel
Hey! I used the view_pre_execute hook and used this: $view->query->pager->set_current_page(<random value>); I set it to random using the range I know the view will have. I am not sure I can get the exact number of pages in the view. Thanks for the help :)
AntonioCS
+2  A: 

Another option would be to randomize the entries in your views. So your page would always be page 1 but it achieves your objective of seeing something different every time come on your site.

In your sort criteria (in the Global Group) add Global: Random -- Randomize the display order.

(Inspired by suggestion at http://mydrupal.com/random_node_or_front_page_in_drupal_like_stumbleupon )

Sid NoParrots
I did know this one, but I really just want to randomize the selected page view and not the view itself
AntonioCS
+1 for a good alternative (even if it did not match the OPs exact requirements - could as well have ;)
Henrik Opel

related questions