Setting drupal view to random page number

I have installed views 2, and I created a view that appears on the first page.

The view displays some links to pages (1 | 2 | 3 | 4 | ... etc.). I want to know if it is possible to start browsing on a random page, and not always start on page 1.

Note. I do not want to randomize the display, which I just want to randomize the loaded page.

thanks

Possible Solution:

In the views_pre_execute method, I used this:

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

I'm not sure that I can determine the number of full pages in the pager at this time, but I will continue the investigation (the $ view object specified in the hook has many properties with arrays and other objects, which makes this difficult)

+4
source share
3 answers

I do not know how to do this from the Views user interface, but you should be able to achieve this using one of the hooks of the view modules , in this case hook_views_pre_execute possible. Unfortunately, the documentation for them practically does not exist, so you will need to implement a hook in the user module and check the transferred view object through the debugger (or print, var_dump, etc.).

You should look for $view->pager['current_page'] , which you can set to a random page. Unfortunately, if I read the code correctly, the counter request, which determines the possible number of pages, is not yet executed, so you will either have to use the "best guess" or come up with another way to determine the correct range for the selection ...

NOTE. This in no way means an "authoritative" answer - just a pointer in which I would start looking, since no one has answered it so far. I could well have missed a more obvious / simple solution: /

+2
source

Another option is to randomize the entries in your views. This way, your page will always be on page 1, but it reaches your goal of seeing something different every time you get to your site.

In your sorting criteria (in the Global group) add Global: Random - randomize the display order.

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

+2
source

I just created a custom pager that automatically jumps to the last page, and I think this is due to what you are trying to do:

In project.info:

 files[] = plugins/views_plugin_pager_last.inc 

In project.module:

 function cvoxm_views_plugins(){ return array( 'pager' => array( 'last' => array( 'title' => t('Paged output, full pager and last by default'), 'short title' => t('Full & Last'), 'help' => t('Paged output, full Drupal style and last by default'), 'handler' => 'views_plugin_pager_last', 'help topic' => 'pager-last', 'uses options' => TRUE, ), ) ); } 

And the contents of the plugins / views _plugin_pager_last.inc:

 class views_plugin_pager_last extends views_plugin_pager_full { function pre_execute(&$query) { if(!isset($_GET['page'])){ // TODO: Should use pager_id // Go to last page $this->set_current_page($this->get_total_items() / $this->get_items_per_page() - 1 ); $this->query(); // Rebuild query $this->update_page_info(); // Update info } } } 
+1
source

All Articles