I'm currently trying to make a json flow from my server but cant print images inside a node.
This is the output ive managed to get:
node1
images - "img1",

node1
images - "img2",

----------------------And this:(stripped-tags)

node1
images - ",",

Tried every combo with views - format settings and field settings but still isn't working.
Anyone has the same problem?

Im using:
Views 7.x-1.x-dev 7.x-3.0-rc1
Views-json 7.x-1.x-dev

NOTE: forgot to mention that its a array from a node with multiple values in one field

Comments

Priority:Critical» Normal
Status:Needs review» Active

sorry for double post... delete this...

Priority:Normal» Critical

*delete*

Status:Active» Needs review

*delete*

Status:Needs review» Active

do you have multiple images in one node in one field?

Yes I do, forgot to mention that.

Priority:Critical» Major
Status:Active» Postponed (maintainer needs more info)

Does anyone know if this works with Views 2?

The code says it's compatible but Views API 2.0. I'm astonished it works that well with Views 3. But I'm experiencing the same problem as @Psyd; multivalue fields always get reduced to just 1 field.

I had the same problem. I was only getting one image of my multi-image field. Here's my quick fix:

Drupal 7.10
Views: 7.x-3.0-rc3
Views JSON: 7.x-1.x-dev

It's in "views_json.module" at line 140:

  // This is the code that's reducing your multiple image field down to a single image field.
  // It's stripping the image url from the $field_output, but only getting the first one, of course.
  // You could use preg_match_all to get each image, but I'm happy getting all the images in the
  // html markup that views creates. I can work with that, so my fix was just to commented out the
  //  following section.
  // Also, this will give you some info about the group settings that someone could use in a better solution:
  // debug($field->options);
  // and more specifically:
  // debug($field->options['group_type']);
  /*
    $img_match = array();
    $src_match = array();
    if (is_array($field_output)) {
      foreach ($field_output as $i => $f) {
        if (preg_match("/<img[^>]+>/i", $f, $img_match)) {
          if (preg_match('/(src)="([^"]*)"/i', $img_match[0], $src_match))
          $field_output[$i] = ($src_match[2]);
        }
      }
    }
    else {
      if (preg_match("/<img[^>]+>/i", $field_output, $img_match)) {
        if (preg_match('/(src)="([^"]*)"/i', $img_match[0], $src_match))
          $field_output = ($src_match[2]);
      }
    }
  */

I got mine to work by using preg_match_all instead of preg_match in the else part where we are looking for image tags. In my case I simply had to turn the values into a JSON list.

<?php
if (is_array($field_output)) {
      foreach (
$field_output as $i => $f) {
        if (
preg_match("/<img[^>]+>/i", $f, $img_match)) {
          if (
preg_match('/(src)="([^"]*)"/i', $img_match[0], $src_match))
         
$field_output[$i] = ($src_match[2]);
        }
      }
    } else {
      if (
preg_match_all("/<img[^>]+>/i", $field_output, $img_match)) {
       
$field_output = '[';
       
$i = 0;
        foreach(
$img_match[0] as $img){
            if (
preg_match('/(src)="([^"]*)"/i', $img, $src_match)){
                if(
$i == 0){
                   
$i++;
                }else{
                   
$field_output .= ', ';
                }
               
$field_output .= ($src_match[2]);
            }
        }
       
$field_output .= ']';
      }
    }
?>

Title:Drupal 7 array not showingFields with multiple values are not displayed
Status:Postponed (maintainer needs more info)» Active

Alright, im gonna test this soon as I have time.
Thanks everybody for your support.

i optimized the code a bit.

i also tried to include the alt text of the images but sadly views_json doesn't accept a construct like this:
$field_output[$i] = array( "src" => "http....", "alt" => "alt text" );

it would be nice to have also the alt and the title text.

<?php
    $img_match
= array();
   
$src_match = array();
    if (
is_array($field_output)) {
      foreach (
$field_output as $i => $f) {
        if (
preg_match("/<img[^>]+>/i", $f, $img_match)) {
          if (
preg_match('/(src)="([^"]*)"/i', $img_match[0], $src_match))
         
$field_output[$i] = ($src_match[2]);
        }
      }
    }
    else {
      if (
preg_match_all("/<img[^>]+>/i", $field_output, $img_matches)) {
        if (
sizeof( $img_matches[0] ) > 1 ) {
         
$field_output = array();
         
$field_is_multiple = TRUE;
          foreach (
$img_matches[0] as $i => $img_match )
            if (
preg_match('/(src)="([^"]*)"/i', $img_match, $src_match))
             
$field_output[$i] = ($src_match[2]);
        } else {
          if (
preg_match('/(src)="([^"]*)"/i', $img_matches[0][0], $src_match))
           
$field_output = ($src_match[2]);
        }
      }
    }
?>

I found a simple work around for this using the Image URL Formatter module. Simply switch the image field's formatter to "Image URL." This will output a comma separated list of urls, bypassing the troublesome code posted in comment #7. Plus, you can still use image styles.

#12 work for me

StatusFileSize
new2.82 KB
new75.92 KB

FYI, as an alternative to comment #12 (but not to discredit it), I am also currently able to output images that reside in multiple-value fields using the field_collection module: http://drupal.org/project/field_collection.

When I am editing a View, inside the "Configure Field" modal for field_collection fields I can choose whether to display the fields in one or multiple lines. Currently I have my field_collection field setup to display in multiple lines - see attached JSON output.

I must admit though, that I do not like a bit that we are doing a preg_match() on field contents to search for <img> tags. Not only it seems not scalable (we are doing a regex on every field, on every single row), it just seems cruft[y] overall. I vote for finding a more elegant way to determine if a field is of type image. Maintainers, please let me know if you feel we should leave it as is or have your mind set on a solution already. I'm going to see if I can roll something out this weekend. Will post a patch first since it might be a big change.

I had the wrong assumption that field_collection was giving the ability to control whether to display multiple items in one views field or not. Field collection is just used for grouping multiple fields in one unit... oops.

After creating a separate and clean baseline site without using the field_collection module I figured out that what we want to do is actually be able to check "Display all values in the same row" and return a JSON collection...

Alexander,

The problem with unchecking "Display all values in the same row" is that it will create duplicates of all the single-value fields, since the entire row is getting repeated. If you have more than one multi-value field the problem is compounded. This can create some seriously bloated JSON. (Believe me, I've been down this road...)

Using Field Collection is an interesting idea. It would be an amazing solution if we could get a nested structure. Something like this:

{
  "nodes" : [
    {
      "node" : {
        "title" : "Example 1",
        "body" : "Donec ullamcorper nulla non metus auctor fringilla.",
        "gallery" : [
          {
            "img" : "http://example.com/sites/default/files/image1.jpg",
            "alt" : "Consectetur Ullamcorper",
            "caption" : "Awesome image"
          },
          {
            "img" : "http://example.com/sites/default/files/image2.jpg",
            "alt" : "Vehicula Vulputate",
            "caption" : "Cool image"
          },
          {
            "img" : "http://example.com/sites/default/files/image3.jpg",
            "alt" : "Inceptos Tellus",
            "caption" : "Nifty image"
          }
        ]
      }
    },
    {
      "node" : {
        ...
      }
    }
  ]
}

I don't think this is currently possible. Anyone know?

The patch in #11 works well for images. Thanks!

-Daniel
Highline Residential Brokerage

I am currently working in a generic solution for all views_datasource plugins. The solution will allow for multi-fields, as well for module developers to extend our rendering process without having to fork or patch the module. Since the change involves major restructuring, I'm going to start committing my changes to my views_datasource sandbox. That way it will be easier for people to test the new code. As soon as I have a writeup I shall post it into a general issue, since this rewrite will solve other issues as well. Stay tuned.

Issue tags:+API change

Tagging.

For those following this issue, I've created issue #1699368: #RVDA: Rewrite Views Datasource's Architecture, which will eventually incorporate a fix for this issue, no ETA. This issue is now a dependency of #1699368.

Status:Active» Postponed
Issue tags:-API change+#VDS-A

I created a separate issue for multiple values not being displayed that are not image specific. http://drupal.org/node/1881670

I can confirm the patch in #11 is also working on views.xml.module (it starts on line 134).

Kind regards,

Julius

StatusFileSize
new65.63 KB

I've been battling this issue for 2 days and #12 is by far the simplest and most accessible fix. @eusonic, I owe you 1 yard of beer! : )

Issue summary:View changes

NOTE: forgot to mention that its a array from a node with multiple values in one field

Issue summary:View changes
Status:Postponed» Needs review
StatusFileSize
new2.73 KB

I've made a patch for this. I still think this whole implementation needs to be refactored to handle images without regex, but this is useful enough now that I thought I should contribute it back.

It incorporates changes from #11 as well as my own changes that add multi-ordinal support to add the alt tag contents as well. This required a modification to views_views_json_style.theme.inc to add support for that.

Hope someone finds this useful. I myself am using it to expose JSON from Drupal to a pure Angular JS frontend.

After reviewing this issue: https://drupal.org/node/913668

I decided to re-roll this with the fixes from the RTBC patch in comment #25 posted over there. The code is intertwined so it's important that these two play nice.

One more time with some more tweaks to make the src/alt approach work right with single images