1

I have a multi-value field. When I output this field in a view I use Rewrite results and reference this field as [field_product_image]. This outputs all the images of the field.

How could I output only certain element of this array, e.g. second image using rewrite results (this is essential)? Neither of these works:

[field_product_image:0], [field_product_image_0], [field_product_image-0]

1
  • are you using drupal 6 or 7?
    – Bala
    Feb 7, 2014 at 12:14

3 Answers 3

1
+50

For that you need to check the Group multiple values and then set to show 1 values and set starting from 1. see the below screenshot for clarification

enter image description here

if you check the rewrite output of this filed you can see the token for that filed, for your case it would probably

[field_product_image_1]

So if you want to retrieve the image separately from the array, you have to do the above steps for each image, for example if you want the 3rd image, then you have to set starting from 2 and its tokes is [field_product_image_2], if you want 4th image, then you have to set starting from 3 and its token is [field_product_image_3].

Below screenshots will help you to understand easily.

I added each filed separately to retrieve each image

enter image description here

and its tokens and above method is for Drupal 6, you can do it in the same way for D7.

enter image description here

8
  • I think this is way more complicated than my answer. Prove me wrong. I'm trying to understand this. First of all, where do you find Group multiple values checkbox? Feb 12, 2014 at 18:48
  • @KariKääriäinen seriously have you never seen this before ? i took screen shot from views
    – Bala
    Feb 13, 2014 at 1:46
  • But how do you get into this dialog? I'm assuming we are talking about D7? Feb 13, 2014 at 5:40
  • @KariKääriäinen It is D6 :)
    – Bala
    Feb 13, 2014 at 5:48
  • Why? I can see you asked OP that but he never answered. Feb 13, 2014 at 5:56
0

I would suggest to create a template specific for this view field, so you can access all your variables with PHP, which is way more convenient to do such things.

So if your field id is "field_product_image", then you should find and download views-view-field.tpl.php, rename it "views-view-field--field-product-image.tpl.php", put your code inside and upload it to your theme/template folder.

You can see in your view, under "Advanced" > "Theme" what is the name to override default views field template and if the overriding template is used.

0

It is assumed in this question that defining which values to output and how to output them would have to happen in the same place, Rewrite results. However, there is nice support for separating these in (D7) Views Configure Field dialog: Multiple field settings for "what" and Rewrite results for "how".

So for D7 use "Multiple field settings". For example "Display 1 starting from 1" outputs the second image. Now you can access your image in "Rewrite results" simply using [field_product_image].

In D6 these settings are in the same dialog, but they work the same way, I believe.

5
  • I need to use Rewrite results. I write some code there using image[1]. Feb 7, 2014 at 15:05
  • Oh OK. Rewriting seems to happen after Multiple field settings so you CAN use it. So in Multiple field settings figure out which images to output, and in Rewrite results figure out how to display them. Feb 7, 2014 at 17:44
  • Well, my question is exactly "to figure out how to display them". Feb 11, 2014 at 16:41
  • You can display them anyway you want using normal methods like "Image style" in the "Field configuration", theming etc. I thought your original question was about how to only display one certain image instead of all images in a multi-value field. Are you asking now about how to for example style the image or have I completely missed something? Feb 11, 2014 at 23:05
  • Or are you wondering what token to use in Rewrite results? It's just the field name, [field_product_image] so no ":0" or "_0" or "-0" or anything like that is needed. I updated my answer. Feb 12, 2014 at 0:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.