0

I have a view that creates JSON for me. The view has a field that multiple images, title, body and some other data fields on it. The image field can have multiple images. How can I show all the images? I know multiple field settings would show all the images but then it would loop all the info multiple times (as many times as there images) and I don't want this to happend.

I have also tried "Display all values in the same row" and putting Simple separator as "," but it only shows the first image.

What I want is the JSON would be something like this:

"nodes" : [

{
  "nid" : "124",
  "title" : "This is the node title",
  "field_shot_desc" : "Short description text",
  "Body" : "<p>Curabitur blandit tempus porttitor. Nulla vitae elit libero, a pharetra augue.</p>\n",
  "field_images" : "http://www.domain.com/sites/mytheme/files/styles/browser-thumb/public/rest1.jpg","http://www.domain.com/sites/mytheme/files/styles/browser-thumb/public/rest2.jpg",

},

So the images would be separated with , or something so I can manipulate them in JS later.

1
  • Please post the current output also.
    – niksmac
    Commented Apr 4, 2012 at 12:00

2 Answers 2

1

Here there is a piece of code I wrote for the same problem. It checks if the field is an array and start a for loop to print the field. I've used this in an external page who gets json through a json view from drupal. I hope this can help you. If you rewrite it in a simple way, please let me know. I'm using drupal 6, with NO "Display all values in the same row".

This Check if the field has got more than one img:

var image =  your_img_field;
    function isArray(image) {
   if (image.constructor == Object) 
      return true;
   else 
      return false;
}

If the field has got more than one img, it counts the array and return the size:

if (isArray(image) == true) {
Object.size = function(image) {
    var size = 0, key;
    for (key in image) {
        if (image.hasOwnProperty(key)) size++;
    }
    return size;
};
var size = Object.size(image);

Here the for loop to print (in my views the array starts from 1)

for (i=1; i<size+1;i++){
$("ul").append("<li><img src=\'"+image[i]+"\' /></li>");
}
}

If the field img has not more than 1 img it prints the single img.

else {
$("ul").append("<li><img src=\'"+image+"\' /></li>");
}

PLEASE, CHECK QUOTES AND DOUBLE QUOTES IN THE IMG TAG <img src=\'"+image+"\' />

2
  • So I think i would need to make a custom module for this? I'm not that experienced with Drupal. I have done mostly admin stuff with it. Commented Apr 4, 2012 at 11:43
  • Where are you using it and why? Commented Apr 4, 2012 at 12:29
0

You should probably use the view template to rewrite the output to fit your needs.

This thread has the information you need to find the correct file, but your use will be slightly different.

It only involves simple PHP string manipulation, probably with an implode somewhere.

Your Answer

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

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