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

Files: 
CommentFileSizeAuthor
#46 views-random-sort-1190900-46.patch1.83 KBphilipz
PASSED: [[SimpleTest]]: [MySQL] 1,658 pass(es).
[ View ]
#46 interdiff.txt1.04 KBphilipz
#35 views-random-sort-1190900-0_3.patch2.08 KBphilipz
PASSED: [[SimpleTest]]: [MySQL] 1,603 pass(es).
[ View ]
#29 views-random-sort-1190900-0.patch2.08 KBls
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch views-random-sort-1190900-0_2.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#27 views-random-sort-1190900-0.patch2.08 KBls
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch views-random-sort-1190900-0_1.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#25 views-random-sort-1190900-0.patch2.09 KBls
FAILED: [[SimpleTest]]: [MySQL] Invalid patch format in views-random-sort-1190900-0_0.patch.
[ View ]
#24 views-random-sort-1190900-0.patch2.09 KBls
FAILED: [[SimpleTest]]: [MySQL] Invalid patch format in views-random-sort-1190900-0.patch.
[ View ]
#13 ViewsMultipleFieldRandomize.patch1.53 KBOniryck
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch ViewsMultipleFieldRandomize.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#10 field.png23.85 KBdawehner

Comments

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 ...

Version:7.2» 7.x-dev

Development happens in dev. ;)

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?

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

Views now handles the integration of Field API data.

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

+1

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.

@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.

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...

StatusFileSize
new23.85 KB

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

Status:Active» Closed (fixed)

@dereine Got it! Thanks for your help :)

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(?))

StatusFileSize
new1.53 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch ViewsMultipleFieldRandomize.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

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 ^^

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.

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

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.

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.

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

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! : )

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

@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.

StatusFileSize
new2.09 KB
FAILED: [[SimpleTest]]: [MySQL] Invalid patch format in views-random-sort-1190900-0.patch.
[ View ]

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.

Status:Needs work» Needs review
StatusFileSize
new2.09 KB
FAILED: [[SimpleTest]]: [MySQL] Invalid patch format in views-random-sort-1190900-0_0.patch.
[ View ]

Again for patch testing.

Status:Needs review» Needs work

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

Status:Needs work» Needs review
StatusFileSize
new2.08 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch views-random-sort-1190900-0_1.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

Trying again.

Status:Needs review» Needs work

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

Status:Needs work» Needs review
StatusFileSize
new2.08 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch views-random-sort-1190900-0_2.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

Status:Needs review» Needs work

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

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.

Status:Needs review» Needs work

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

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

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

StatusFileSize
new2.08 KB
PASSED: [[SimpleTest]]: [MySQL] 1,603 pass(es).
[ View ]

This one should pass.

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.

a very needed feature, this patch worked beautifully

Great patch,
works perfect, exactly what I needed!

Many thanks

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

Status:Needs review» Reviewed & tested by the community

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

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

Confirmed Patch #35 works as intended

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.

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.

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?

Status:Needs work» Needs review
StatusFileSize
new1.04 KB
new1.83 KB
PASSED: [[SimpleTest]]: [MySQL] 1,658 pass(es).
[ View ]

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

Status:Needs review» Reviewed & tested by the community

I can confirm this still works as expected.

Can also confirm that it works as intended still.

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

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