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?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

junedkazi’s picture

One 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

tsvenson’s picture

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

junedkazi’s picture

Did you select a filter. Select a filter to sort only those node type which has this unlimited
upload field.

tsvenson’s picture

The imagefield does not show up under content in filters.

merlinofchaos’s picture

Project: Views (for Drupal 7) » ImageField
Version: 6.x-2.5 » 6.x-3.0-rc1
Component: node data » Miscellaneous

This would be up to imagefield.

quicksketch’s picture

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

tsvenson’s picture

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

drewish’s picture

i think the random image from a node might be the hard part. you can easily specify just the first file though.

mathmeye’s picture

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

tsvenson’s picture

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

tsvenson’s picture

Project: ImageField » Content Construction Kit (CCK)
Version: 6.x-3.0-rc1 » 6.x-2.2
Component: Miscellaneous » Views Integration

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

mathmeye’s picture

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

  1. Download version 6.x-2.x-dev of CKK dated April 21st.
  2. Replace the file cck/includes/views/handlers/content_handler_field_multiple.inc with the one attached.

Let me know if that works for you. I'll submit a proper patch soon.

tsvenson’s picture

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

tsvenson’s picture

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

tsvenson’s picture

mathmeye,

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.

tsvenson’s picture

mathmeye,

Disregard #15, it worked in the end, just took several hours before the default image was generated...

mathmeye’s picture

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

mathmeye’s picture

Title: Random pick from imagefield with unlimited uploads in a block » Random pick from multiple value field in views
Status: Active » Needs review

Changed title since this should apply for all kinds of multiple value cck fields and marked for review.

KoCo’s picture

Thx,

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

RobW’s picture

Thank you, this is exactly what I was looking for.

RobW’s picture

Is 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?

aidinnz’s picture

I love it, it works perfectly and it was easy to patch! Cheers!

ayalon’s picture

Version: 6.x-2.2 » 6.x-2.6
Status: Needs review » Reviewed & tested by the community
FileSize
1.67 KB
19.29 KB

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

quicksketch’s picture

Category: support » feature
Status: Reviewed & tested by the community » Needs work

This patch isn't truly ready.

There's no matching opening div on this:

+      '#suffix' => '</div>',

There are no form options for 'random_from', but the logic seems to anticipate such options.

quicksketch’s picture

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

jo1ene’s picture

+1

I used it just now and it works great. Very useful and simple to implement.

ayalon’s picture

Status: Needs work » Needs review
FileSize
1.88 KB

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

OldAccount’s picture

This patch does exactly what I needed -- randomize field display vs randomize entire node. This should definitely be added to CCK. Thank you!

MattFielding’s picture

Fantastic patch. Just what i was looking for.

farrington’s picture

This patch was exactly what I was looking for.

Added to the CCK-6.x-2.x-DEV, please! :-)

Anonymous’s picture

Thank you very much, this is a simple but incredibly useful patch. Why not yet reviewed and included in CCK?
Thank you again.

sabrawy’s picture

it's not working with latest cck dev after 2.8 also im using views 3

aidinnz’s picture

Status: Needs review » Reviewed & tested by the community

Please 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?

antiorario’s picture

Any news on where this is going?

caraiba’s picture

Unfortunately even with the D7-Views this issue is unresolved. Why? I have no idea.

imclean’s picture

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

imclean’s picture

Status: Reviewed & tested by the community » Needs work

Updated status.

aidinnz’s picture

Version: 6.x-2.6 » 6.x-2.9

I don't understand why this feature was removed in 2.8+

circuscowboy’s picture

Version: 6.x-2.9 » 6.x-2.x-dev
Status: Needs work » Needs review
FileSize
2.08 KB

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

Bartezz’s picture

Just tested #39 and it solves my needs for a random order. Haven't encountered any bugs...

Cheers

Branndon’s picture

#39 worked for me! I had a text box, and I wanted a random value to be selected. Thanks!

Bartezz’s picture

Status: Needs review » Reviewed & tested by the community
sammyd56’s picture

This is exactly what I need for a current project. Could anyone explain how this could be done in Drupal 7?

Geijutsuka’s picture

Status: Reviewed & tested by the community » Needs review

I just implemented #39 patch and the random image works just great... except when I'm not logged in. Is anyone else experiencing this bug?

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

willeaton’s picture

Can we get this committed into the module please?

KarelK’s picture

I 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?

drupalfan81’s picture

This 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!

Danny Englander’s picture

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

broncomania’s picture

D7: I think it's really missing. It makes it impossible to make image galleries without this feature.

c3info’s picture

It's late but for the record for CCK 3.X add:

in function option_definition()...
$options['multiple'] = array(
+ 'multiple_random' => array('default' => FALSE),

szy’s picture

#39 still works for me against 6.x-2.9.

Thank you!

Szy.

Talkless’s picture

Missing this for Drupal 7

beardedgeek’s picture

Issue summary: View changes

Is this in 7 yet?

Katy Jockelson’s picture

For 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