Comments

jonathanmd’s picture

I just ran into a similar problem when trying to combine multiple column fields. Whenever the field contains an img tag the entire contents of that field get replaced with only the source of that image.

I'm making an RSS feed and combining a text summary and image into an element called description and wrapping it with CDATA so that it still validates. However, I keep running into the problem above where the entire contents of the field are replaced with the img src.

Line 133 of views_datasource.module (similar replacements occur in the other .module files)

  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 was going to create a patch for this that only replaces the img with its source and not the entire field, but I couldn't think of a usecase where you'd want to replace images with the src. Developers already have ability to select whether or not to use the entire image or just the image path from within the view.

Is there a reason to always have images stripped out or can that part of the code just be removed?

allisterbeharry’s picture

Assigned: Unassigned » allisterbeharry

jonathanmd is right - it was a quick hack to solve an issue in the Imagecache module that should only be inserted when selected as an option. Will be fixed in the next release.

pschopf’s picture

This issue presents several interesting problems. We ran into this when trying to feed nodes from one site to another. Any field with an Only local images are allowed. tag in it would reduce to the contents of the src attribute. But the choice of leaving the whole Only local images are allowed. item in the field gives us additional problems: If the src attribute is a relative address (src="/sites/default/files/xyz.png") any import of this field to another site will probably produce a missing image. If the src attribute is an absolute address, then the exported field would be fine. The option of removing all Only local images are allowed. elements would be OK for some, but probably not for others.

Would it be OK to convert all relative addresses to absolute?

michellezeedru’s picture

Subscribing. Has a fix for this problem been committed to the dev version yet? Are there any recommendations for what I can do until there is a fix?

Thanks!

steel-track’s picture

Has any headway been made on this? It is a pretty serious issue on a project I am working on, but I don't want to start making patches if it's already being addressed. I am currently using the dev version from Feb. 25th.

chromix’s picture

Seconding #5. I've currently just commented out $img_match = array(); down to the end of the if block.

span’s picture

I'm not sure if this is related but it is truly a weird problem that I haven't really dug into yet.

I've got a view that creates some JSON output. It seems that as soon as the JSON runs into an image in my node->body field it takes the image into the JSON source and then it stops. This leads to any body field that begins with an image and then has some body text will only show up in the JSON as the image path. When there is some text before and after an image, the image path is still the only thing that shows up in the JSON output.

Setting the node body to "strip html tags" makes all the data come into the JSON object but I do need the tags so I can't use this workaround. Also tried using the dev version with no luck.

Looked into this a bit and ended up with this that solved my immediate problem:

views_json.module line 133

    /*$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]);
      }      	    	
    }*/

Removing this piece of commening does the trick on getting all the contents back for me. Surely it is not a good solution for everyone but could be useful for anyone with a specific need.

jaarong’s picture

I'm running into this same issue. I found that switching to raw output instead of normal fixes the issue, but I'm not sure exactly why I have to do that and what that means for other outputted data.

fangel’s picture

I too ran into this issue, and while I understand the reason for this functionality, I find it too aggressive in it's replacement.

Say you have a media-field, then it's very likely that you would JSON field represented as the images source, not a Only local images are allowed.-tag. But in all the use-cases described here (basically a html-field that contains Only local images are allowed.s)

My proposed fix is to change the Only local images are allowed.-regex to only match if the entire field-output is the Only local images are allowed.-tag. This maintains the media-field src-substitution functionality while still allowing html-fields to contain images.

    $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]);
      }      	    	
    }
guybedford’s picture

Great module!

I also had to comment out these lines, as I'm outputting full node data with views node field (drupal.org/project/viewsnodefield).

floydm’s picture

@allisterbeharry - I'd be happy to try to create a patch for this if I understood when that block of code is necessary. For the time being I'm doing what everyone else is doing and just commenting it out.

-Floyd

hbfkf’s picture

Subscribing.

bigjim’s picture

We're experiencing this in the 7.x dev branch also

biteuljiouce’s picture

same for me, I'm trying to get the content (html) of a text field of a node. This field may contain both text and images. And I couldn't get the whole.

here are some examples :

source content :

<p>&nbsp;</p>
<p>encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp; &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp; &nbsp; &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;</p>
<p>&nbsp;</p>
<p><img alt="" class="diaporama_enabled" src="http://www.pierrelapolice.local/cms/sites/default/files/resize/field/image/%20bssc0255-devon_aoki-lachapelle-300x415.jpg" title="" width="300" height="415" /></p>
<p>&nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp; &nbsp;&nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp; &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;</p>
<p>&nbsp;</p>

field format "raw" (I'm getting only text) :

"&nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp; &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp; &nbsp; &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;&nbsp;&nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp; &nbsp;&nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp; &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;encore du texte &nbsp;&nbsp;"

field format "default" (img url only) :

"http://www.pierrelapolice.local/cms/sites/default/files/resize/field/ima..."

field format "cut = 600" :

<p> </p>\n<p>encore du texte encore du texte encore du texte encore du texte encore du texte encore du texte encore du texte encore du texte encore du texte encore du texte encore du texte encore du texte encore du texte encore du texte encore du texte encore du texte </p>\n<p> </p>

field format "cut = 650"
"http://www.pierrelapolice.local/cms/sites/default/files/resize/field/image/%20bssc0255-devon_aoki-lachapelle-300x415.jpg"

(I installed Drupal in french, so I'm gessing the name of the field format).

biteuljiouce’s picture

Version: 6.x-1.0-beta2 » 7.x-1.x-dev
Issue tags: +json output

maybe we could check what type of field it is ?
with Drupal 7 (maybe 6 ?), i can test the value of field_info['module'].

if ($view->field[$field->options['id']]->field_info['module'] == 'image')
{
    // ... put the code here
}
jyraya’s picture

Conversion of relative addresses to absolute, mentioned by pschopf, seems to be a good solution. Nevertheless, if we consider the question of performances, that could be tricky. the conversion treatment increases the view rendition time and it is especially true for sites with a huge number of concurrent users and a lot of content to render.

For me, this treatment, if requested, should rather be done at node edit/creation time than here.

lisa.rae’s picture

Hey Guys,

We ran into the same problem on a project where we were setting up a web service for an iPad app, and pulling down information to store locally on the iPad through the json feed. The problem we ran into is that we had some fields that were storing uploaded images in an Image field, which we needed to download from the image URLs, and we had some content that had imbedded <img> tags in the body field using a wysiwyg editor, which was getting munged by the code block in question.

When we commented out the block of code, we were no longer getting the information on the images stored in an Image field, but our body fields with imbedded <img> tags was coming across fine.

Our solution is to replace the block of code referenced in comment #9 above with the following:

    if(isset($field->field_info)) {
      if($field->field_info['type'] == 'image') {
        $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]);
          }
        }
      }
    }

We're now getting the URLs for images that are uploaded into an image field, and we're preserving the HTML content, including imbedded <img> tags, in the body fields. We haven't tested this for all media types, but I suspect that it should work ok. If someone else could test as well and confirm that this is a workable solution, we'll prepare and submit a patch.

Lisa

jrglasgow’s picture

I have resolved this on my system by editing it to call another function that searches for all img tag sources, checks to make sure the url is absolute, and if not it makes it absolute.

Here is my patch against 7.x-1.x

Anonymous’s picture

tried patch in number #18 first but this only works if you don't have single image fields. otherwise it breaks stuff.

if you have single image fields but also text fields containing images use #17 it works nice for me.

thanks allot!

rajab natshah’s picture

Seems that last fix code is not a fix for each Field states.
I have this patch for it.


   // if type of output is not an image.
    if ($field->options['type'] != 'image' ) {
        $img_match = array();
        $src_match = array();
        
        // if we have number of values for this field.
        if (is_array($field_output)) {
          foreach ($field_output as $i => $f) {
            preg_match_all("/<img[^>]+>/i", $f, $images);
            // if we have more than one image in the field.
            if (is_array($images)) {
              foreach ($images[0] as $image) {
                $img_match_item = array();
                $src_match_item = array();

                if (preg_match("/<img[^>]+>/i", $image, $img_match_item)) {
                  if (preg_match('/(src)="([^"]*)"/i', $img_match_item[0], $src_match_item)) {
                    $field_output[$i] .= '<img src="' .($src_match_item[2]). '" /> ';
                  }
                }
              }
            }
            else {
              // for one image in the field.
              if (preg_match("/<img[^>]+>/i", $f, $img_match)) {
                if (preg_match('/(src)="([^"]*)"/i', $img_match[0], $src_match)) {
                    $field_output[$i] .= '<img src="' .($src_match[2]). '" /> ';
                }
              }
            }
          }
        }
        else {
          preg_match_all("/<img[^>]+>/i", $field_output, $images);
          if (is_array($images)) {
            foreach ($images[0] as $image) {
              $img_match_item = array();
              $src_match_item = array();
              
              if (preg_match("/<img[^>]+>/i", $image, $img_match_item)) {
                if (preg_match('/(src)="([^"]*)"/i', $img_match_item[0], $src_match_item)) {
                  $field_output .= '<img src="' .($src_match_item[2]). '" /> ';
                }
              }
            }
          }
        }
    }
    else {
      // if field's type was an image. we just ge the path.
      if (preg_match("/<img[^>]+>/i", $field_output, $img_match)) {
        if (preg_match('/(src)="([^"]*)"/i', $img_match[0], $src_match)) {
            $field_output .= ($src_match[2]);
        }
      }
    }

rajab natshah’s picture

Do no use this patch it's was for the master branch .. Obs ... the patch file in comment #22 is the right one. it's for the 7.x-1.x branch.

rajab natshah’s picture

This is the right patch file for views_datasource-7.x-1.x to have many images in html field and image path for image fields

rajab natshah’s picture

rajab natshah’s picture

Seems that different View's Options will effect the output ..

This code will work. for HTML .. in Default View ..

    $img_match = array();
    $src_match = array();   
    if (is_array($field_output) && $field->options['type'] == 'image') {
      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 = ($src_match[2]);
          }
        }
      }
    }
    else if ($field->options['type'] == 'image' ) {
      // if field's type was an image. we just ge the path.
      if (preg_match("/<img[^>]+>/i", $field_output, $img_match)) {
        if (preg_match('/(src)="([^"]*)"/i', $img_match[0], $src_match)) {
            $field_output = ($src_match[2]);
        }
      }
    }

We should fix this not to have img tags or any other tags in none HTML Fields.

emattias’s picture

Here's the same patch as #24 but it avoids notices when $field->options['type'] doesn't exist.

ishmael-sanchez’s picture

Status: Active » Reviewed & tested by the community

The patch in #25 works great. Tested it with multiple images.

zserno’s picture

Indeed, patch in #25 worked for me as well.

ConradFlashback’s picture

This problem runs also with xml.

ken hawkins’s picture

Seconding that this fix works. Would be great to see committed.

jaymallison’s picture

Related: https://drupal.org/node/1319714#comment-8681645

I made a patch to support multiple images in a field and add sub ordinals including the src and alt attributes. The code from that issue is joined at the hip with the code from this issue so I merged the patch from #25 with my original patch.

I'll post the patch here too so if anyone is interested in testing this - please give feedback.

jaymallison’s picture

New patch that fixes the src/alt for single images.

batpurev’s picture

comment #25 worked for me like a charm for D7.27 thanks a lot guys!

ndf’s picture

Just created JSON field formatter module.
It provides a field formatter for text fields that outputs encoded json. This works for body fields and could be useful for a bunch of above issues.

technicalknockout’s picture

Was experiencing this issue. So far #31 worked for me without issues.

b33tl3’s picture

Same for me.
I need to output text field with html markup and many images (coming from media, inserted into wysiwyg with insert module).
views_datasource json outputs only a single img path and get rid of all other html and text.
#31 worked for me. MANY THANKS

PS : would be nice to commit this patch :)

jaymollica’s picture

Hey everyone, not sure how useful this is, but I altered the views_json.module file to work with the image_field_caption module so that you can now associate image captions to the images in the json feed. I've attached the modified views_json.module, the caption stuff starts around line 188 and uses the same regex method to grab the captions, which I agree is not ideal. But it works in a pinch.

      elseif ($field->options['type'] == 'image' ) {

        //check for image captions as constructed by project image_field_caption
        if (preg_match_all("/<blockquote[^>]*class=\"image-field-caption\">(.*?)<\\/blockquote>/si", $field_output, $cap_matches)) {
          foreach($cap_matches[1] AS &$cap) {
            //clean up the captions for the json feed
            $cap = htmlspecialchars(trim(strip_tags($cap)));
          }
        }

        // if field's type was an image. we just get the path.
        if (preg_match_all("/<img[^>]+>/i", $field_output, $img_matches)) {
          $field_output = array();
          if (sizeof($img_matches[0]) > 1) {
            $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'] = $src_match[2];
                $field_output[$i]['alt'] = '';
                $field_output[$i]['cap'] = $cap_matches[1][$i];
                if (preg_match('/(alt)="([^"]*)"/i', $img_match, $alt_match)) {
                  $field_output[$i]['alt'] = $alt_match[2];
                }
              }
            }
          }
          else {
            if (preg_match('/(src)="([^"]*)"/i', $img_matches[0][0], $src_match)) {
              $field_output['src'] = $src_match[2];
              $field_output['alt'] = '';
              $field_output['cap'] = $cap_matches[1][0];
              if (preg_match('/(alt)="([^"]*)"/i', $img_matches[0][0], $alt_match)) {
                $field_output['alt'] = $alt_match[2];
              }
            }
          }
        }
      }
mrded’s picture

Just trying to apply #31 patch

git apply --index multi_value_images_with_embedded_image_fix-1319714-26_0.patch
multi_value_images_with_embedded_image_fix-1319714-26_0.patch:43: trailing whitespace.
}
multi_value_images_with_embedded_image_fix-1319714-26_0.patch:60: trailing whitespace.
}
multi_value_images_with_embedded_image_fix-1319714-26_0.patch:114: trailing whitespace.
} else {
multi_value_images_with_embedded_image_fix-1319714-26_0.patch:121: trailing whitespace.
}
multi_value_images_with_embedded_image_fix-1319714-26_0.patch:122: trailing whitespace.
}
warning: 5 lines add whitespace errors.

mrded’s picture

#25 & #31 patches need to follow Drupal code standards

issa.haddadin’s picture

Title: Views_Json replaces html with inline img tag's src attribute » Views_Json & views_xml replaces html with inline img tag's src attribute
Issue tags: +xml output
StatusFileSize
new1.45 KB

The same problem is happening on views_xml.

I used the last patch from @mrded and implemented on views_xml and worked fine with me.

briand44’s picture

This is the same patch as #39 just applies against the latest dev.

alarcombe’s picture

There's a fundamental problem with using the field type as an indicator for whether to rewrite an image field in that it doesn't work if you use an image field as a source for a rewritten field. The rewritten field will not be of type 'image' and so will be rendered differently than the source field.

alarcombe’s picture

Addressing the issue of rewritten fields I mentioned in #41, the attached patch provides a configuration option for the views_datasource display which determines whether views_datasource reformats the output of all fields that *start* "<img " or just those which are image fields. It's not perfect, but is more configurable than the previous resolutions to this issue.

kenorb’s picture

Status: Needs work » Needs review
akeemw’s picture

StatusFileSize
new1.45 KB

Adjusted the patch in #39 to work against the current release - 7.x-1.0-alpha2

berdyshev’s picture

patch in #42 works for me, thanks