Problem/Motivation
Sort: global random is refreshed, when a pager is used. The expected behavior is to have results of the view sorted randomly, and display them in that order no matter the pager configuration, but at the moment it is showing random nodes on each pager page and even show the same nodes on the different pages.
Steps to reproduce
- Install Drupal
- Create 50 dummy pages
- Create a view (either page or block)
- Set the view to show the field title (or teaser content)
- Add sort criteria as Global: Random (asc)
- Make sure no other sort criteria is listed
- Add a pager to show 5 items (Full | Paged, 5 Items)
Navigate to the view that you have created. This should results in showing 50 unique nodes being randomly sorted and displayed 5 per page.
What actually happens when clicking through pager is that we get duplicates appearing. It would appear that the pager is requesting 5 nodes and sorting these by random. The next pager repeats this request, resulting in duplicates being pulled through. The nodes are not seeded in a sequence between pages.
Using AJAX makes no difference the issue is present in both AJAX and Non AJAX views.
Proposed resolution
This will probably not be fixed in Drupal core.
As a workaround, you can use https://www.drupal.org/project/views_random_seed if you use Search API Solr. But it does not work with standard Search API database backend: #3229841: Not working with search_api.
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #28 | screen-capture (1).gif | 2.14 MB | kostyashupenko |
Comments
Comment #2
lendudeThis would be a new feature (and indeed better handled in contrib I feel).
Yes the random sort is applied anew for every page. Yes this leads to strange results when combining random sorting with a pager. The core random sort is only usable for simple 'show a set number of random things' use case.
Comment #3
sittard commentedOK - I've put a request in to port the contrib Drupal 7 Views Random Seed Module (https://www.drupal.org/node/2820021) to Drupal 8. But as that module owner has not released an update in 5 years I'm not holding out much hope for this happening quick.
Any other workarounds would be appreciated.
Comment #4
dawehnerYeah, I don't have too much hope either ;)
Comment #5
sittard commentedOK so we now have a working patch (see https://www.drupal.org/node/2820021) - big credit to @dawehner
I'm tending to disagree with @lendude on this:
If we offer 'random' sort in Views core then really it should work regardless of whether the view has a pager or not. The site builder shouldn't need to install a contrib module to get it working.
Thanks to all for moving this forward.
Comment #6
dawehnerIt is technically a feature ... as it doesn't exist yet in the module. Also keep in mind that it might be exactly what people expect it to do at the moment. Random is random.
Also I think having a pager and random is more of an edge case. I think the way more common usecase is to just show a single item on the frontpage, for example, in which case this feature doesn't matter.
Comment #7
marassa commentedFirst of all, huge thanks to dawehner for the working solution!
And I tend to agree with sittard that this really should be in the Views core. It might be an edge case but it certainly isn't for me and my users: I store thousands of tagged pictures and let users select all pictures having a certain tag (from a tag cloud). I need a pager because the result list can be very long. I would prefer a random order so that the user doesn't start with the same old set of pictures any time they would like to revisit the view (on another day). But seeing the same picture(s) reappearing again and again while paging through the view is frustrating and not at all what my users expect. In case someone actually prefers such behavior ("random is random") then I suppose there should be a checkbox whether to use seed or not, effectively merging the functionality of two handlers into one.
So I urge the module maintainers to give it one more think ;)
Comment #10
tom3b commentedDefinitely feel this should be marked as a Bug and not a Feature Request. I can't think of a reason why pagination should re-randomise the data. The whole premise of pagination is to cater for when you have more data than you can display on one page, the next logical page should continue in the same order as the first.
Comment #11
tetranz commentedI think the obvious question to anyone thinking about implementing this is: How?
The randomness data (sort order / weight) would need to persist between requests so where would that be stored? It would need to be saved in a temporary table or something. Saving it in session is possibly an option but that might have caching issues and there's probably a maximum size issue.
How long should the random sort order persist? If I go page 1, page 2 and back to page 1 then page 1 should be unchanged but if I come back to page 1 tomorrow should it still be the same? I guess that needs to be configurable. i.e., a "randomness lifetime" setting or maybe it's unique to the user session. i.e., logout and login and get a different order.
This is quite a difficult issue to solve and it probably means different things to different people.
Comment #12
marassa commentedThis obvious question has an obvious answer: https://www.drupal.org/project/views_random_seed
And it is already implemented. And it actually works.
Comment #15
handkerchiefany news on this?
Comment #17
msypes commentedCurrently (2020-04-22) the recommended contrib module, Views random seed = https://www.drupal.org/project/views_random_seed, is in alpha, and hasn't been updated in over two years.
Also, this thread looks like a duplicate of https://www.drupal.org/project/views/issues/1131940
Comment #19
back-2-95Now that Drupal 8 is released views_random_seed is not an option anymore.
Site cannot be upgraded to D9 with Composer if views_random_seed is installed.
Comment #20
maskedjellybeanThis is definitely a bug. Random sort and pagination are both core views functionality and yet when you combine them what you get is 10 links at the bottom of your view that all do the exact same thing.
I was able to get views_random_seed to work with Drupal 8, but the lack of maintenance and a Drupal 9 release is concerning. I'll cross that bridge later...
Comment #26
john.glynn.davies commentedLooks like the Views Random Seed module is now available for D8, D9 and D10.
https://www.drupal.org/project/views_random_seed
Comment #28
kostyashupenkoBump. This is still an issue in Drupal 10.2
I just have generated several nodes of some content type -> then I created new view with random sorting (no matter if Ajax enabled or not).
Look at `16` number on the gif above, it's often appears on several pages.
Moreover, here is another use case.
Let's say we have some taxonomy terms of some vocabulary. And some terms has same weight. And guess a view which is displaying those terms with the sorting only by weight (no matter asc or desc and no matter if ajax enabled). In the end some of the terms will be duplicated on different pages.
Comment #29
marassa commented@kostyashupenko, the core developers clearly don't agree that this is an issue. And with a contrib workaround available - https://www.drupal.org/project/views_random_seed - who cares?
Comment #30
ressaUpdating the Issue Summary. Can this issue be closed, since there is a solution available? Feel free to re-open if not.
Comment #32
ressaAdd note that https://www.drupal.org/project/views_random_seed/ only works with Solr.