A similar issue was started quite some time ago for the CCK project (actually, I think it was started in the Views issues, then moved to CCK for integration with Views).

I'm surprised it still hasn't made it into CCK in D7. I figured perhaps this issue should be mentioned for core, since most of CCK ended up in there.

This is about integration with the Views Module. We can sort results in views randomly at the node level (for instance, sorting content of type Gallery into a random order). However, we cannot sort at the field level (for instance, sorting the images in each Gallery's image field randomly).

My use case is that I have a bunch of short 'tips' on certain subjects. It would be ugly to create a separate piece of content for each one, so I have them all housed in a single piece of content, as separate entries in a text field. I would like to create a view that will display a single random entry.

I can create a view that will only display the first entry, or the last entry, because the field entries can be reversed under Multiple Field Settings in Configure Field. It would be great if the entries could be randomized.

Please see a similar issue in the CCK module: http://drupal.org/node/439230

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

george.plescan’s picture

I tried to convert the patch done for CCK and I'm not sure of the end result. I have no errors, but I think it is not working.

Basically this is what I modified : views_handler_field_field.inc in views\modules\field.

Here I found similar lines to the ones in the CCK patch.

I added the $form['multiple_random'] on line @505 and changed the incompatible values from 6.x to the ones from $form['delta_reversed'], because they are quite the same.

Also I registered the multiple random option at @320 like :

$options['multiple_random'] = array(
'default' => FALSE,
);

so I don't get a "not found" error.

Everything is fine so far but I don't know what to do with the actual function and where to put it ...

I added the random function somewhere at @645 but after some tests it doesn't seem to work, it just show the first item.

Hope we get some help for this considering that before the patch was for CCK 6.x and now I modified Views in 7.x ...

dddave’s picture

Version: 7.2 » 7.x-dev

Development happens in dev. ;)

baby.hack’s picture

Hehe, thanx dddave. I suppose any feature requests should be for a dev version, rather than simply pointing out that the feature isn't in the current version?

yched’s picture

Project: Drupal core » Views (for Drupal 7)
Version: 7.x-dev » 7.x-3.x-dev
Component: field system » fieldapi data

Views now handles the integration of Field API data.

kclarkson’s picture

I too would love this feature. Random image field for example.

+1

dawehner’s picture

Status: Active » Fixed

Uncheck "Display all values in the same row" in the field settings in views and you get a new result per imagefield.

Once you have this you can use the global: random sort.

kclarkson’s picture

@dereine,

thanks for the quick reply.

Here is what I am trying to do in views. I have nodes that have multiple images. In the teaser version I would like to have just one of the images show, but have the images randomly change. At the same time I would like to sort each block node by date so the random piece would have to be in the field vs. sorting the nodes randomly.

I tried your suggestion but did not see that option in the image field settings.

Something similar to this module but within views would be cool as a feature: http://drupal.org/project/field_multiple_limit

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

sammyd56’s picture

Status: Closed (fixed) » Active

Re-opening as I also can't find that option and have a similar use-case to #7. Pretty sure we're not the only ones looking for random sorting at a field level...

dawehner’s picture

FileSize
23.85 KB

This configuration is on the field itself. You can see the configuration on the screenshot linked in the issue.

sammyd56’s picture

Status: Active » Closed (fixed)

@dereine Got it! Thanks for your help :)

andjules’s picture

Status: Closed (fixed) » Active

re-opening; unless I'm misunderstanding, the solution doesn't really address the original request, as I understand it.

Assumptions:

  • view returns a list of nodes, showing various fields per node ("Show: Fields", not "Show: Content")
  • one of the fields (let's say an image field) allows multiple values
  • for some nodes of this type, the field may have no value (unpopulated), for others there may be one value, for others still there may be multiple values
  • goal: when returning the view, make sure any nodes with multiple values only show one (image, in this case), but choose it randomly

When - as suggested above - I deselect "Display all values in the same row", that node now appears multiple times (once for each of the values in the mutiple-value-field) in the returned view. Adding the global random to the sort seems to just randomize the order in which those repeated nodes appear. Adding 'distinct' to the SQL settings has no effect.

What intuitively makes sense is under the field settings > multiple field settings > to add a 'randomize' button (frankly, I can't see how this isn't a more obvious/common choice vs the current ' first and last only' checkbox, for which I struggle to imagine a use case(?))

Oniryck’s picture

Hello,

I'm really interested in this feature. I don't understand why it isn't in the main module yet. I wrote the attached patch to implement this feature. Could it be considered to be added in Views as a feature ? Maybe this patch is the wrong way of doing this, or maybe there's already a way to do this, in which cases I'd really like to know more about it :)

I tried to do this with an external module too, but overriding the handlers of all fields doesn't sound like a good idea !

I'm a beginner in Drupal and this my first contribution, but I'm willing to know more if anybody has a better idea. Thank you for your understanding ^^

Jack3rror’s picture

This patch worked for me, as I would like to shuffle the multiple field.

Thx. I wonder if I could see a module based on this patch, to learn how to contribute to drupal.

risharde’s picture

Requesting this as part of main module, we really need to have the ability to randomize on a multi field...

dawehner’s picture

Status: Active » Needs review
--- views_handler_field_field.inc	2012-04-16 11:43:38.000000000 +0200
+++ views_handler_field_field.inc	2012-05-31 18:05:20.119843136 +0200

Please patch against the root directory of views, so for example the testbot can have a look at the patch and do its automatic testing

+++ views_handler_field_field.inc	2012-05-31 18:05:20.119843136 +0200
@@ -313,6 +313,11 @@
+    ¶

Some nitpicking: You introduced some empty lines with just spaces in your patch.

Set to needs review so the testbot jumps in.

Status: Needs review » Needs work

The last submitted patch, ViewsMultipleFieldRandomize.patch, failed testing.

broncomania’s picture

Status: Needs work » Needs review

#13: ViewsMultipleFieldRandomize.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, ViewsMultipleFieldRandomize.patch, failed testing.

philsward’s picture

Would love to see this completed. I've been waiting close to a year to see this implemented... It would also greatly increase the functionality of Views Slideshow. I was able to get this accomplished with Views on D6 but I can't remember which site I did it to, to go back and see what I did to make it work... I could have sworn I either used the delta or fid as either an argument or relationship, but neither seem to do anything right with Views 3

In my use case situation, I've created a content type "Front Page Image". I create ONE node to upload all of my images to. I want to only show ONE image out of the fields but have it show up randomly. It would go hand-in-hand with views slideshow by allowing a person to create a single node for all of their banner images to rotate through randomly. It would also expose all images for a gallery of multiple nodes, to show up randomly instead of just the first image in the gallery. It would also grant flexibility to a text field where a person wants to only show one text field at a time, but have the order show up randomly. Think of a quote node where the text field contains the actual quotes. Put a block on the home page and have it randomly rotate through the multiple fields on visit to the home page. It eliminates the need for multiple nodes.

Idealy, there needs to either be a checkbox for "random" in the Multiple Field Settings or allow the starting from: to accept random as an input instead of just the fid/delta number.

Just my $0.02

philsward’s picture

Grr...!! I KNEW there was a way to do it!!

1) Simply uncheck the Display All Values in the Same Row
2) Set the sort to Global: Random
3) Set the PAGER to Display a specified number of items with that number being 1

DONE

Been jacking with this for 2.5 hrs...

Dear Devs:
Please make this a bit more straight forward / easier! : )

aspilicious’s picture

Dear philsward, feel free to make a patch that makes this more straight forward.

philsward’s picture

@aspilicious If I knew how to code, It would have already been done and I wouldn't have previously posted a front end "work around"... Sorry, not trying to be rude, just get frustrated when devs tell me to "throw some code at it" after I point out a trouble area. If I knew how to fix it, I wouldn't be crying out for help and if I had the money, I would just pay someone else to fix it for me.

ls’s picture

I'm sorry, but this is not a solution to the problem that the OP described.
Because I ran into the same problem I created a patch to add a random order checkbox to the multiple value field.

ls’s picture

Status: Needs work » Needs review
FileSize
2.09 KB

Again for patch testing.

Status: Needs review » Needs work

The last submitted patch, views-random-sort-1190900-0.patch, failed testing.

ls’s picture

Status: Needs work » Needs review
FileSize
2.08 KB

Trying again.

Status: Needs review » Needs work

The last submitted patch, views-random-sort-1190900-0.patch, failed testing.

ls’s picture

Status: Needs work » Needs review
FileSize
2.08 KB

Status: Needs review » Needs work

The last submitted patch, views-random-sort-1190900-0.patch, failed testing.

GuyPaddock’s picture

Status: Needs work » Needs review

This patch worked for me. Not sure why patch felt the line was improperly terminated; it actually ended up applying okay.

aspilicious’s picture

Status: Needs review » Needs work

Your editor needs configuration, let the editor convert tabs into 2 spaces, and strip trailing line endings

yul63’s picture

@philsward
Thank you, you just saved me 2 hours :)

kclarkson’s picture

Does this patch need to be rewritten to pass before we test it?

philipz’s picture

This one should pass.

grozozo’s picture

Status: Needs work » Needs review

I just applied the #35 patch and it's working perfectly. Thx philipz and all of you for this.

I think that this function should be included in view. It's a little thing but save hours of work. So I pass the status to need review.

srgk’s picture

a very needed feature, this patch worked beautifully

maxplus’s picture

Great patch,
works perfect, exactly what I needed!

Many thanks

PhilY’s picture

@philipz
Thanks a lot for patch at #35, it works under Views 7.x-3.7

Summit’s picture

Status: Needs review » Reviewed & tested by the community

Hi, this works great, setting it to RTBC, ok?
Greetings, Martijn

Homotechsual’s picture

Any idea if/when this will be committed to Views 7/D8 Core? It's a nice feature which really solidifies Views handling of multivalue fields

wingalingadingdong’s picture

Confirmed Patch #35 works as intended

Neograph734’s picture

Is there any update on this? It's such a small feature, but there are so many possible use cases... Would love to see this in Views Core.

jrreid’s picture

Yep. I will see if I have a chance this week to port it to D8, and hopefully then we can get it also included in D7. Its indeed a very helpful and needed feature.

dawehner’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs work
+++ b/modules/field/views_handler_field_field.inc
@@ -757,6 +769,20 @@
+      $array_values = array_values( $values );
+      shuffle( $array_values );
+      $random = array();
+      $key = 0;
+      foreach ($array_values as $array_value) {
+        $random[$key] = $array_value;
+        $key++;
+      }

We should at least use the drupal default code style here, but is it just me that this is is quite some code to get a random array? isn't there at least something like array_rand?

philipz’s picture

Status: Needs work » Needs review
FileSize
1.04 KB
1.83 KB

@dawehner you're absolutely right. I guess I've re-rolled the patch without much thinking ;)

Neograph734’s picture

Status: Needs review » Reviewed & tested by the community

I can confirm this still works as expected.

Homotechsual’s picture

Can also confirm that it works as intended still.

litwol’s picture

Works as intended. i hope this can get into --dev soon.

Neograph734’s picture

@dawehner, you think you could commit this to views?

reallyordinary’s picture

This stopped working for me after the most recent Views security update. Tried patching again to no effect. Tried patching against most recent dev version, also no effect.

EDIT: Oops - disregard; re-applying patch DID work - but the "Random" checkbox in the view had lost its setting and needed to be clicked back on again.

petiar’s picture

I have also successfully applied this patch, so what can I do to help to get it in the .dev version of Views?

Thanks.

Jej’s picture

#46 tested successfully too. I use random sort on an image field formatted as colobox gallery, works as expected.

Thanks for this 17 lines patch, quite efficient.

I wish I would to see it in Views.

shortspoken’s picture

#46 works as expected. Thanks @philipz. Please get this committed into views!

colan’s picture

We've recently switched our testing from the old qa.drupal.org to DrupalCI. Because of a bug in the new system, #2623840: Views (D7) patches not being tested, older patches must be re-uploaded. On re-uploading the patch, please set the status to "Needs Review" so that the test bot will add it to its queue.

If all tests pass, change the Status back to "Reviewed & tested by the community". We'll most likely commit the patch immediately without having to go through another round of peer review.

We apologize for the trouble, and appreciate your patience.

Neograph734’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
1.83 KB

Re-uploading as requested. Simplytest.me applied the patch, so hopefully it passes. Credits to philipz for the original patch.

Neograph734’s picture

Status: Needs review » Reviewed & tested by the community

Re-marking RTBC from #47 as test passed.

  • colan committed f9d7151 on 7.x-3.x authored by ls
    Issue #1190900 by ls, philipz, Neograph734, Oniryck: Random sort from...
colan’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 7.x-3.x-dev » 8.1.x-dev
Component: fieldapi data » views.module
Status: Reviewed & tested by the community » Patch (to be ported)

Thanks!

Neograph734’s picture

Status: Patch (to be ported) » Needs review
FileSize
1.65 KB

I believe this should do for Drupal 8.

Neograph734’s picture

Oops, copied the comment number in the last one... Same patch different name.

The last submitted patch, 61: views-random-sort-1190900-57.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 62: views-random-sort-1190900-62.patch, failed testing.

Neograph734’s picture

Status: Needs work » Needs review
FileSize
2.24 KB

Added the delta_random option to the yml file.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

bwoods’s picture

This patch generally works with 8.4, but it needs to be applied to EntityField.php, instead of Field.php, which is being depreciated.

There are a few small syntax changes as well - using brackets instead of array calls, but it still works. Thanks for creating this patch!

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Tobias März’s picture

Created patch to work with Drupal 8.5.

Status: Needs review » Needs work

The last submitted patch, 73: views-random-sort-1190900-64.patch, failed testing. View results

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

BrightBold’s picture

The patch in #73 appears to need a reroll, but in the meantime, the tip in #21 works for Drupal 8 as well.

anmolgoyal74’s picture

Status: Needs work » Needs review
FileSize
2.02 KB

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
FileSize
2.11 KB

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

_utsavsharma’s picture

Tried to fix CCF for 10.1.x in #77.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.