Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I have a content type where it is possible to upload unlimited number of images. I have successfully been able to create a block that only shows one image. Now I try to figure out how I can get view2 to randomly select one of the x images for that content field in the block.
Is this possible?
Comments
Comment #1
junedkazi CreditAttribution: junedkazi commentedOne can do it using the sort Random option.
What one has to do is the following.
1- In the view which you have created add a sort criteria.
2 - In the sort criteria form the groups select Global
3 - once Global is selected it will display the random display option.
You can select that option and you are ready to go.
Hope it helps.
Thanks
Juned Kazi
Comment #2
tsvenson CreditAttribution: tsvenson commentedI tried to add the global random to the block view, but it also showed up on all other views... I suppose it should as its market global, but for the Block and Default it was not italic and for the other two it was italic.
Been trying to figure out how to use it for the imagefield (with unlimited uploads) but I can't find any option.
Looking at a few other posts I get the feeling global/random is a node level sort. What I need is a way to randomly select/sort a multi field type and display one of the items on content level.
What I did was:
- Added a the image field to the Block view
- Selected "Group multiple values" and entered 1 in the Show field, leaving the other two blank.
That now shows only 1 image, however it will always be the first image uploaded using imagefield. This is where I would like views to randomly select for example one of the x images uploaded to that node.
Comment #3
junedkazi CreditAttribution: junedkazi commentedDid you select a filter. Select a filter to sort only those node type which has this unlimited
upload field.
Comment #4
tsvenson CreditAttribution: tsvenson commentedThe imagefield does not show up under content in filters.
Comment #5
merlinofchaos CreditAttribution: merlinofchaos commentedThis would be up to imagefield.
Comment #6
quicksketchDon't filter by Imagefield, filter by "Node: type" and filter down to the type that contains the image field. Then use the sort "Global: random" to pull out a random row.
Comment #7
tsvenson CreditAttribution: tsvenson commentedquicksketch - I would be very grateful if you could give me a step by step guide on how to do that.
What I am trying to do is to create a block that will show, for example, a block teaser of five random nodes (same content type). In every node I have an imagefield which is set to unlimited uploads and I want the block to show a random image from that field.
I am sorry, but it seems to me that with views I have to do some things in a specific order to be able to create this.
Comment #8
drewish CreditAttribution: drewish commentedi think the random image from a node might be the hard part. you can easily specify just the first file though.
Comment #9
mathmeye CreditAttribution: mathmeye commentedI had the same issue. Random sort applies to node level (random record out of found results), not field level (random field entry out of every result) - however, I am using an older version of imagefield, so this might not hold true anymore.
In the end, I made some changes to the cck views handler as attached. It adds an option 'Random' to fields with multiple entries.
Up to now you could have: Select x entries starting from y and optionally inverse order.
Now you can: Select x random distinct entries from the choice of y to last entry
This is only a quick hack and does not check if x <= last-y, hence it could loop infinitely. I was planning on submitting that to cck after some rework, please let me know your thoughts.
Comment #10
tsvenson CreditAttribution: tsvenson commentedNice one mathmeye. I will test it, just need to figure out how to patch on a windows system and upload it to the hosting first.
Comment #11
tsvenson CreditAttribution: tsvenson commentedTrying to apply this patch. Have got unxUtils working, but get "can't find file to patch at input line 4" no matter where I try to apply the patch, even when I change the patch in the patch itself and try to apply it in the same dir. I have followed all the steps and read the common patch problems on d.o, but it simply don't want to work.
Would be great if you can email me the patched file using my contact form.
I am using all the latest official versions of involved modules.
Also moving this to CCK...
Comment #12
mathmeye CreditAttribution: mathmeye commentedYes, sorry for that. As I said, the above was only to hear your thoughts on the matter and not meant as a valid patch. Since I am atm on windows, I just attach the complete file.
Let me know if that works for you. I'll submit a proper patch soon.
Comment #13
tsvenson CreditAttribution: tsvenson commentedI actually got the patch working after reading the --help :), but since I am on CCK 2.2 I will have to apply it manually. I know PHP, but this is virtually the first time I dig into Drupal code so we will see how it goes.
Comment #14
tsvenson CreditAttribution: tsvenson commentedAll right, perfect. Works exactly like I want it. Many thanks mathmeye, that made my day :)
Please let me know when you have implemented the "check if x <= last-y" as the site I am working on is going live shortly and your patch might not have made it to an official CCK release by then.
Here is a working file for CCK 6.x-2.2.
Comment #15
tsvenson CreditAttribution: tsvenson commentedmathmeye,
Found a smallish problem with your random function. If the node does not have any images, any defailt image set for the content type will not be displayed when using your random function.
Comment #16
tsvenson CreditAttribution: tsvenson commentedmathmeye,
Disregard #15, it worked in the end, just took several hours before the default image was generated...
Comment #17
mathmeye CreditAttribution: mathmeye commentededde42, sorry for the late answer, if you could please try the following. should work for all conditions, please let me know the outcome. patch is agains the 6-2 branch.
Comment #18
mathmeye CreditAttribution: mathmeye commentedChanged title since this should apply for all kinds of multiple value cck fields and marked for review.
Comment #19
KoCo CreditAttribution: KoCo commentedThx,
This is a great feature, especially when you want to maintain nodes as containers of multiple values.
To the end-user it is sometimes wise to offer the ability to create a photoalbum with one link, instead of the numerous modules and add-ons which mostly require to create a node container and add image nodes separatly.
This worked to get one random item of a multiple value field.
However, it broke my 2x2 grid view. This was probably more due to my settings. I'm currently testing with one node, and displayed four items to get a nice 2x2 grid. For this I had to leave the group multiple function out. The strange thing is, it already displayed four random pictures from the same multiple value field this way. I wonder? If you just leave the grouping out, and only use the global random. But you'll probably get four items from the first node and none of the others you want to display. (#2 Btway, you can override this by editing the criteria and then deleting it from the default display)
I'll make sure to check this with more nodes when I go live, but for now I recommend adding the feat. It's simple enough and doesn't seem to interfere with anything else.
Koen
Comment #20
RobW CreditAttribution: RobW commentedThank you, this is exactly what I was looking for.
Comment #21
RobW CreditAttribution: RobW commentedIs there a way to select multiple field values in a view like this? As an example, I have two imagefields on story posts, one for teaser thumbnails and an unlimited one for post body images. I'd like to make a block with random images from both of these fields linked back to their nodes; right now using the random patch and limit to display one it works great for the body images, but if I add the teaser image as a view field the two imagefields are always displayed together, like "Random article 1 (TeaserImg, random BodyImg), Random article 2 (TeaserImg, random BodyImg)". I'm guessing this is because Global: Random sorts by nodes, and the random patch sorts inside a field. I'd like to be able to have a random assortment of both, i.e. random article's teaser images or body images displayed singly and mixed up with each other.
Any ideas? Is this possible through views, or would I need some custom php written?
Comment #22
aidinnz CreditAttribution: aidinnz commentedI love it, it works perfectly and it was easy to patch! Cheers!
Comment #23
ayalon CreditAttribution: ayalon commentedYet another patch that never was reviewed nor committed by the cck code maintainer.
I re-rolled this patch against CCK 2.6. It's a simple and useful patch.
Use case:
- Display a random image from a node with multiple images attaches
- Diplay a random search term from a node with multiple search terms
At the moment its only possible to randomize the node order with the Global: Random option in views sorting.
This patch enables you to display a random value from a multivalue field.
Please commit this patch, enough time has passed an there are enough testers, that confirm that this functionality is needed.
Comment #24
quicksketchThis patch isn't truly ready.
There's no matching opening div on this:
There are no form options for 'random_from', but the logic seems to anticipate such options.
Comment #25
quicksketchSorry I misread "multiple_from" as "random_from", so there's no problem there. The patch isn't quite following coding standards (spaces within the parenthesis of control statements are not required).
Comment #26
jo1ene CreditAttribution: jo1ene commented+1
I used it just now and it works great. Very useful and simple to implement.
Comment #27
ayalon CreditAttribution: ayalon commented@quicksketch
You are right, there were two closing div tags. Its correct, that there is only a #suffix because the opening tags is in another form item. See the attached screenshot.
I corrected this.
Could you please check it again?
Comment #28
OldAccount CreditAttribution: OldAccount commentedThis patch does exactly what I needed -- randomize field display vs randomize entire node. This should definitely be added to CCK. Thank you!
Comment #29
MattFielding CreditAttribution: MattFielding commentedFantastic patch. Just what i was looking for.
Comment #30
farrington CreditAttribution: farrington commentedThis patch was exactly what I was looking for.
Added to the CCK-6.x-2.x-DEV, please! :-)
Comment #31
Anonymous (not verified) CreditAttribution: Anonymous commentedThank you very much, this is a simple but incredibly useful patch. Why not yet reviewed and included in CCK?
Thank you again.
Comment #32
sabrawy CreditAttribution: sabrawy commentedit's not working with latest cck dev after 2.8 also im using views 3
Comment #33
aidinnz CreditAttribution: aidinnz commentedPlease fix this on 2.8. This whole feature has dissappeared from 2.8. Was it not released with 2.6? Why is 2.8 missing this feature?
Comment #34
antiorario CreditAttribution: antiorario commentedAny news on where this is going?
Comment #35
caraiba CreditAttribution: caraiba commentedUnfortunately even with the D7-Views this issue is unresolved. Why? I have no idea.
Comment #36
imclean CreditAttribution: imclean commentedThe patch in #27 still doesn't address quicksketch's comments in #25 regarding spaces. If you don't understand, he's just asking you to remove the extraneous spaces from between brackets.
Comment #37
imclean CreditAttribution: imclean commentedUpdated status.
Comment #38
aidinnz CreditAttribution: aidinnz commentedI don't understand why this feature was removed in 2.8+
Comment #39
circuscowboy CreditAttribution: circuscowboy commentedThank you mathmeye and ayalon for your work and revisions.
This patch is just what the doctor ordered
I have updated the patch per quicksketch and imclean request (removed extraneous spaces and ran it against the latest 6.x-2.x branch)
This patch address a wonderful little feature that I have need a couple of times already.
If this patch gets included I will even add the little documentation so that the rest of the world will understand this new feature.
Comment #40
Bartezz CreditAttribution: Bartezz commentedJust tested #39 and it solves my needs for a random order. Haven't encountered any bugs...
Cheers
Comment #41
Branndon CreditAttribution: Branndon commented#39 worked for me! I had a text box, and I wanted a random value to be selected. Thanks!
Comment #42
Bartezz CreditAttribution: Bartezz commentedComment #43
sammyd56 CreditAttribution: sammyd56 commentedThis is exactly what I need for a current project. Could anyone explain how this could be done in Drupal 7?
Comment #44
Geijutsuka CreditAttribution: Geijutsuka commented...Is what I was going to say, and then it started working, but the "randomness" is significantly slower for anonymous users than it is for authorized ones.
Comment #45
willeaton CreditAttribution: willeaton commentedCan we get this committed into the module please?
Comment #46
KarelK CreditAttribution: KarelK commentedI first tried to implement a patch version #39 with CCK 6.x-2.9
Unsuccessfully.
Next I tried the version of CCK 6.x-2.x-dev, 6.x-3.x-dev version with other patch version here on the page.
Unsuccessfully.
Using the patch I added chackbox "random" but it is a check, update and save views, again unchecked. Field image show always the first image of the node images. Still only default function "Show 1 value (s) starting from 0."
Can you please advise where I'm wrong?
Comment #47
drupalfan81 CreditAttribution: drupalfan81 commentedThis is great! And a well needed feature built into CCK as many of us build content types with fields of multiple values and for photo albums this feature would come in very useful. Has there been any traction to get this added? If I have the most up to date CCK with D6, how can I get this functionality installed and working. I need this functionality for a project I am currently working on. Thanks!
Comment #48
Danny EnglanderI just ran up against needing this in Drupal 7 so I ended up using a JQuery shuffle plugin. It's not ideal at all though becuase I then had to hide anything that was
element:not(:first)
so the plugin only gets you half way there. If anyone is interested, PM me and I can point you in the right direction of how I did it. Cheers.Comment #49
broncomania CreditAttribution: broncomania commentedD7: I think it's really missing. It makes it impossible to make image galleries without this feature.
Comment #50
c3info CreditAttribution: c3info commentedIt's late but for the record for CCK 3.X add:
in function option_definition()...
$options['multiple'] = array(
+ 'multiple_random' => array('default' => FALSE),
Comment #51
szy CreditAttribution: szy commented#39 still works for me against 6.x-2.9.
Thank you!
Szy.
Comment #52
Talkless CreditAttribution: Talkless commentedMissing this for Drupal 7
Comment #53
beardedgeek CreditAttribution: beardedgeek commentedIs this in 7 yet?
Comment #54
Katy Jockelson CreditAttribution: Katy Jockelson commentedFor D7 I've got this working:
Within Multiple Field Settings for my image field that has multiple values, I've unticked 'Display all values in the same row'. That seems to let the 'Global: Random (asc)' in the overall Sort Criteria have the desired affect. Then I just limited the pager options to 'Display a specified number of items | 6 items' because I want 6 random images to show from the many uploaded to that field.
Hope that helps someone