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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Anonymous’s picture

Priority: Critical » Normal
Status: Needs review » Active

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

Anonymous’s picture

Priority: Normal » Critical

*delete*

Anonymous’s picture

Status: Active » Needs review

*delete*

Pasqualle’s picture

Status: Needs review » Active

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

Anonymous’s picture

Yes I do, forgot to mention that.

PieterDC’s picture

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.

francismacomber’s picture

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]);
      }
    }
  */
KimmoT’s picture

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.

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 .= ']';
      }	  
    }
Pasqualle’s picture

Title: Drupal 7 array not showing » Fields with multiple values are not displayed
Status: Postponed (maintainer needs more info) » Active
Anonymous’s picture

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

philippsimon.de’s picture

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]);
		}
      }
    }
?>
eusonic’s picture

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.

xandeadx’s picture

#12 work for me

Alexander Allen’s picture

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.

Alexander Allen’s picture

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

eusonic’s picture

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?

kerberos’s picture

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

-Daniel
Highline Residential Brokerage

Alexander Allen’s picture

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.

Alexander Allen’s picture

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.

Alexander Allen’s picture

Status: Active » Postponed
Issue tags: -API change +#VDS-A
cbrasfield’s picture

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

juliusvaart’s picture

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

Kind regards,

Julius

bisonbleu’s picture

FileSize
65.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! : )

bisonbleu’s picture

Issue summary: View changes

(unnecessary edit)

jaymallison’s picture

Issue summary: View changes
Status: Postponed » Needs review
FileSize
2.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.

jaymallison’s picture

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.

jaymallison’s picture

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

Jibus’s picture

#26 works for me on a json feed.

Got a couple of errors when applying the patch

git apply -v multi_value_images_with_embedded_image_fix-1319714-26.patch
multi_value_images_with_embedded_image_fix-1319714-26.patch:43: trailing whitespace.
          }
multi_value_images_with_embedded_image_fix-1319714-26.patch:60: trailing whitespace.
          }
multi_value_images_with_embedded_image_fix-1319714-26.patch:114: trailing whitespace.
          } else {
multi_value_images_with_embedded_image_fix-1319714-26.patch:121: trailing whitespace.
              }
multi_value_images_with_embedded_image_fix-1319714-26.patch:122: trailing whitespace.
            }
Checking patch views/theme/views_views_json_style.theme.inc...
warning: views/theme/views_views_json_style.theme.inc has type 100755, expected 100644
Checking patch views_json.module...
warning: views_json.module has type 100755, expected 100644
Hunk #1 succeeded at 183 (offset 12 lines).
Applied patch views/theme/views_views_json_style.theme.inc cleanly.
Applied patch views_json.module cleanly.
warning: 5 lines add whitespace errors.

Thanks !

chrsnlsn’s picture

is this patch supposed to make a multi image field work correctly?
I have a multiple image field and it's still only putting one image in the json output.

japerry’s picture

Status: Needs review » Fixed

Marking fixed, I've cleaned up the code a bit and committed #26. If there are further issues we can open new issues or re-open this.

  • japerry committed 88a8a8c on 7.x-1.x authored by jaymallison
    Issue #1319714 by jaymallison, Alexander Allen, bisonbleu: Fields with...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

cscott5288’s picture

I have a multi-field collection which allows multi-images and I am unable to correctly output JSON with the latest dev release.

liquidcms’s picture

Status: Closed (fixed) » Active

Is this actually fixed? If so what is the trick to getting it working?

I have a multi entityref field (tabs) which leads to multiple values settings option for that view field. if i display in single row with comma separator i get json that looks like this:

      "node" : {
        "title" : "Specials",
        "body" : "<p>DANISH</p>\n<p>Vestibulum a sem sem. Praesent ac pharetra augue! Pellentesque eu tortor semper, scelerisque odio nec, molestie odio.</p>\n<p>Vivamus rutrum, tortor eu cursus accumsan, nibh sapien sodales metus, eget elementum sem augue vel eros. Donec ac posuere.</p>\n",
        "image" : {
          "src" : "http://mma.liquidcms.org/sites/default/files/images/sections/info%402x.png",
          "alt" : ""
        },
        "subtitle" : "Specials",
        "tabs" : "696,700,705,700,705",
        "nid" : "680"
      }
    },

as i would sort of expect i get a CSV list of nids, which when json decoded returns NULL. is there a format setting which maps this to a json array?

kenorb’s picture

Status: Active » Closed (fixed)

Create a new ticket then if you think it's still the problem, with more details.

Vikash raj’s picture

can it be possible to do rest export of node which have multiple field with image. i need image url in array format.