Jump to content

Community

Multiple upload field URLs incomplete


snugRugBug
 Share

Recommended Posts

This happens in IPS 4.1.5.1. I'm currently trying to pull image URLs out of a multiple upload field. The key for the field is 'images'. Here are a few dumps of some sample data:

{{var_dump($record->customFieldDisplayByKey('images', 'raw'));}}

/*
Outputs:
string(444) "http://localhost/1/uploads/monthly_2015_12/action-cameras.jpg.1cb0d20295d6f5ede9fb1bb123cb4a1e.jpg,monthly_2015_12/altimeters.jpg.7bd7495b7a007bc250f7f287ddb3d551.jpg,monthly_2015_12/clothing.jpg.b6c6739d679e7a8456714a7d8f6b564d.jpg,monthly_2015_12/main-canopies.gif.02de5e7b06afdf05f0c4eb927b870a3c.gif,monthly_2015_12/videos-and-books.jpg.3d82d15d241067b5140ed75d323f2349.jpg,monthly_2015_12/wingsuits.jpg.e7db045cb170ef531185d9ecf7d611c9.jpg" 
*/

{{var_dump($record->customFieldDisplayByKey('images', 'processed'));}}

/*
Outputs:
object(IPS\Http\Url)#345 (5) { 
	["url":protected]=> string(444) "http://localhost/1/uploads/monthly_2015_12/action-cameras.jpg.1cb0d20295d6f5ede9fb1bb123cb4a1e.jpg,monthly_2015_12/altimeters.jpg.7bd7495b7a007bc250f7f287ddb3d551.jpg,monthly_2015_12/clothing.jpg.b6c6739d679e7a8456714a7d8f6b564d.jpg,monthly_2015_12/main-canopies.gif.02de5e7b06afdf05f0c4eb927b870a3c.gif,monthly_2015_12/videos-and-books.jpg.3d82d15d241067b5140ed75d323f2349.jpg,monthly_2015_12/wingsuits.jpg.e7db045cb170ef531185d9ecf7d611c9.jpg" 
	["data"]=> array(3) { 
		["scheme"]=> string(4) "http" 
		["host"]=> string(9) "localhost" 
		["path"]=> string(428) "/1/uploads/monthly_2015_12/action-cameras.jpg.1cb0d20295d6f5ede9fb1bb123cb4a1e.jpg,monthly_2015_12/altimeters.jpg.7bd7495b7a007bc250f7f287ddb3d551.jpg,monthly_2015_12/clothing.jpg.b6c6739d679e7a8456714a7d8f6b564d.jpg,monthly_2015_12/main-canopies.gif.02de5e7b06afdf05f0c4eb927b870a3c.gif,monthly_2015_12/videos-and-books.jpg.3d82d15d241067b5140ed75d323f2349.jpg,monthly_2015_12/wingsuits.jpg.e7db045cb170ef531185d9ecf7d611c9.jpg" 
	} 
	["queryString"]=> array(0) { } 
	["isInternal"]=> bool(true) 
	["isFriendly"]=> bool(true) 
} 
/*

2 Problems:

 

1. All of the URLs are in one string. This is not a huge problem, but generally one expects to be able to do this:

{{$images = $record->customFieldDisplayByKey('images', 'raw');}}
{{foreach $images as $image_url}}
	<div class="my-container"><img src='{$image_url}' /></div>
{{endforeach}}

 But you can't. You must first split up the string like this:

{{$images = explode( ',', $images);}}

This isn't a huge problem, but does not seem like the system was intended to work the way it's working now.

 

2. This is the bigger problem of the 2. Only the first URL in the long URL string can be used directly. The rest of the URLs are incomplete. In my example dump, all of the URLs after the first one need at least 'uploads/' added to the front to be used. To summarize, one would expect this code to work:

{{$images = explode( ',', $images);}}
{{$images = $record->customFieldDisplayByKey('images', 'raw');}}
{{foreach $images as $image_url}}
	<div class="my-container"><img src='{$image_url}' /></div>
{{endforeach}}

But all the images, except the first one, don't have working links.

Please let me know if this is intended behavior, or a bug.

Link to comment
Share on other sites

Just now, snugRugBug said:

@eden buganim what should I be passing into $storageExtension?

The storage extension used when uploading the images. should be something like 'AppName_ExtensionClass'.

You can try finding the file where the upload field is created, that'll tell you the storage extension right away, as its used in the 'options' array.

Link to comment
Share on other sites

Found it! In My case the storage extension is 'cms_Records'. Everything works now, although I still feel like the original problem behavior should be fixed.

Thanks @eden buganim

Does anyone else think that the original problem warrants a bug report?

In previous versions (IIRC 4.1.5), the URLs were at least complete, even though they were still all mushed up into one string.

Link to comment
Share on other sites

Yes, but it wouldn't it make more sense to store the URLs in such a way they can be used directly, eg 

/1/uploads/monthly_2015_12/altimeters.jpg.7bd7495b7a007bc250f7f287ddb3d551.jpg

instead of

monthly_2015_12/altimeters.jpg.7bd7495b7a007bc250f7f287ddb3d551.jpg

? It's not much longer, and it means that I don't have to call 

\IPS\File::get( $storageExtension, $url )->url;

for every URL.

Wouldn't it also make more sense to return this kind of field as an array of image objects/URLs, since we are working with multiple items that we generally want to loop through? 

Edited by snugRugBug
Link to comment
Share on other sites

5 minutes ago, snugRugBug said:

Wouldn't it also make more sense to return this kind of field as an array of image objects/URLs, since we are working with multiple items that we generally want to loop through? 

I would report a bug.... that's what the custom formatting fields state should occur, and that was working as suggested in 4.0.x.

Link to comment
Share on other sites

6 minutes ago, RocketSMS said:

As a newbie to IPS I am having trouble following what you finally decided for getting the images to display.  Could you post the final code you used to display images from multiple uploads in a pages database?  Thanks. 

{{$images = $record->customFieldDisplayByKey('images', 'raw');}}
{{$images = explode( ',', $images);}}
{{foreach $images as $image}}
	{{$url = \IPS\File::get( 'cms_Records', $image )->url;}}
     <img src='$url' />
{{endforeach}}
    

I assumed you already have the $record variable available. Let me know if I'm wrong.

Hope this helps. I'm only barely starting to master IPS myself.

Link to comment
Share on other sites

This worked as expected.  Thanks.  

For those following, I just needed to replace 'images' in the first line of his snippet just above with my Template Key shown on the Display Options tab under Editing the Field (in my case I used photos).  For my project I tested this by pasting this code into the Display Custom Format box (after selecting the Custom radio button).  Each of the photos displayed and it got around the problems discussed in this thread.

Link to comment
Share on other sites

I had been looking in the documentation and forum for hours to find ways to use the pages/CMS database upload field for images and to display them within the record listing.  Thanks to @snugRugBug with help from @eden buganim and @Marcher Technologies this now works quite easily.  For those that are beginners like me, let me post my solution to display the images using the IPS Lightbox slider at the bottom or top of the post depending upon the option selected:

<section data-controller='core.front.core.lightboxedImages' >
  <p class='p1'>Enlargeable Images via Lightbox Slider: </p>
  <p>
{{$images = $record->customFieldDisplayByKey('photos', 'raw');}}
{{$images = explode( ',', $images);}}
{{foreach $images as $image}}
	{{$url = \IPS\File::get( 'cms_Records', $image )->url;}}
     <a class='ipsAttachLink ipsAttachLink_image' href='$url' >
       <img src='$url' class='ipsImage ispImage_thumbnailed' alt='$url' style='max-height: 160px'/>
    </a>
{{endforeach}}
  </p>
</section>

The above code is inserted into the Display Custom Format box after selecting the Custom radio button under Editing Field > Display Options tab.  My template key is 'photos' and you need replace the 'photos' in the $record line with your Template Key.

Just FYI I am building a 'classifieds light' for my members using the pages/CMS capability that allows members of certain groups (paid in the future) to post their own classified ads.  The advice on this thread put me over the hump and it's looking pretty good. Thanks again.

Link to comment
Share on other sites

Since the Display Custom Format box executes even if there are no uploads I needed to handle the null case.  Unfortunately, the current version returns a string so I wanted to find a clean way to verify that no images had been appended.  I added a test for string length.  To keep this snipped complete I am reposting it with the additional test for the uploads null case.

{{$images = $record->customFieldDisplayByKey('photos', 'raw');}}
{{$c = $_SERVER['SERVER_NAME'];}}
{{$images = explode( ',', $images);}}
{{if strlen($images[0])>strlen('http://'.$_SERVER['SERVER_NAME'].'/uploads/')}}
<section data-controller='core.front.core.lightboxedImages' >
  <p class='p1'>Enlargeable Images via Lightbox Slider: </p>
  <p>
{{foreach $images as $image}}
	{{$url = \IPS\File::get( 'cms_Records', $image )->url;}}
     <a class='ipsAttachLink ipsAttachLink_image' href='$url' >
       <img src='$url' class='ipsImage ispImage_thumbnailed' alt='$url' style='max-height: 160px'/>
    </a>
{{endforeach}}
  </p>
</section>
{{endif}}

Note that normally exploded $images returns $images[0] = "http//www.example.com/uploads/" for the null case.  I could count the number of characters for my specific case to see if a file was appended but thought comparing with the length of 'http://'.$_SERVER['SERVER_NAME'].'/uploads/' would be more reusable and readable.

One still needs to replace 'photos' with the template key for this field in this snippet.

Link to comment
Share on other sites

  • 3 weeks later...

My code broke with latest upgrade.  Appreciate that you linked your input here.  Prefer not to hard code in '27' so that the code is reusable ... but extending your code with comments from @opentype we can simplify further, make it highly readable, and reusable.

{{if !empty($formValue)}}
<section data-controller='core.front.core.lightboxedImages' >
   <p>
      {{foreach $value as $pic}}
         <a class='ipsAttachLink ipsAttachLink_image' href='$pic' >
            <img src='$pic' class='ipsImage ispImage_thumbnailed' alt='$pic' style='max-height: 160px'/>
         </a>
      {{endforeach}}
   </p>
</section>
{{endif}}

This works for empty uploads case.  Thanks for advice.

Link to comment
Share on other sites

On 15 December 2015 at 2:03 AM, snugRugBug said:

{{$images = $record->customFieldDisplayByKey('images', 'raw');}}
{{$images = explode( ',', $images);}}
{{foreach $images as $image}}
	{{$url = \IPS\File::get( 'cms_Records', $image )->url;}}
     <img src='$url' />
{{endforeach}}
    

 

On 4.1.6 and 4.1.7 this is now causing an EX2 page error anywhere this code is used.  Any ideas?

Link to comment
Share on other sites

The initial bug is now fixed. Thus, something like the following should work:

{{$images = $record->customFieldDisplayByKey('images', 'raw');}}
{{foreach $images as $image_url}}
	<div class="my-container"><img src='{$image_url}' /></div>
{{endforeach}}

You can basically just throw out the explode.

I will test and confirm ASAP

Link to comment
Share on other sites

3 minutes ago, snugRugBug said:

The initial bug is now fixed. Thus, something like the following should work:


{{$images = $record->customFieldDisplayByKey('images', 'raw');}}
{{foreach $images as $image_url}}
	<div class="my-container"><img src='{$image_url}' /></div>
{{endforeach}}

You can basically just throw out the explode.

I will test and confirm ASAP

Magic, worked a treat....

<section data-controller='core.front.core.lightboxedImages' >
  {{$images = $record->customFieldDisplayByKey('gpsimgs', 'raw');}}
  {{foreach $images as $image_url}}
    <a class='ipsAttachLink ipsAttachLink_image' href='{$image_url}' >
      <img src='{$image_url}' class='ipsImage ispImage_thumbnailed' alt='$url' style='max-height: 34px'/>
    </a>	
  {{endforeach}}
</section>

 

 

 

Link to comment
Share on other sites

 Share

×
×
  • Create New...

Important Information

We use technologies, such as cookies, to customise content and advertising, to provide social media features and to analyse traffic to the site. We also share information about your use of our site with our trusted social media, advertising and analytics partners. See more about cookies and our Privacy Policy