Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Showing images from UploadField in custom template


Reply


4 Posts   309 Views

Avatar
Martinjj

Community Member, 12 Posts

30 December 2013 at 7:41am

Hi there,

I have a DataExtension, which is named in _config.php as:
Product::add_extension('ProductImages');

this is done to extend the "Product" class which in turn is a page type that extends Page.php.

I have also created an "UploadField" in ProductImages.php

<?php
class ProductImages extends DataExtension {

private static $db = array(
);

private static $many_many = array(
'ProductImages' => 'Image'
);

public function updateCMSFields(FieldList $fields) {
$fields->addFieldToTab(
'Root.UploadImages',
$uploadField = new UploadField(
$name = 'ProductImages',
$title = 'Upload 1-8 images'
)
);

$uploadField->setAllowedMaxFileNumber(8);
$uploadField->setFolderName('products');
}
}

the CMS for the Product page has the new tab (Product Images) and the images upload to the the assets/uploads/products folder no problem and are held in the database.

My problem is getting the images to show at the frontend,
using:

<ul>
<% loop $ProductImages %>
<li>$SetWidth(100)</li>
<% end_loop %>
</ul>

or

<ul>
<% loop $ProductImages %>
<li>$Image.SetWidth(100)</li>
<% end_loop %>
</ul>

return nothing,

i am guessing i am missing something out in my ProductImages.php file, being a SS3.1 virgin i have been stuck deep in the docs and cannot find as to where i am going wrong, any pointers would be appreciated.

thanks

Avatar
camfindlay

Forum Moderator, 171 Posts

30 December 2013 at 11:02am

I copied your extension code and attached the DataExtension to my Page pagetype and it all seems to work. Note, I used the following front end template code from your example (your 2nd example does work as there is no object called "Image", you are already looping through Image objects with "ProductImages"):

<ul>
<% loop $ProductImages %>
<li>$SetWidth(100)</li>
<% end_loop %>
</ul>

Perhaps check to ensure in your code that there is nothing in your Product page type causing an issue or that the page in the site tree is indeed set to the page type that your DataExtension is applied to.

Only other thing I could think of is you might need to flush your template cache with a good old "?flush=1" added to the URL on the frontend of your site.

Avatar
Martinjj

Community Member, 12 Posts

30 December 2013 at 11:29am

Thanks so much for testing Camfindlay.. i dont realy know what to say, i have probably done 50 or so ?flush=1, dev/builds and reworking the bit of code, i took time out to work on a custom login form for the site, noticed your reply while looking something up and went back to the Product Extension for one more go... and it worked without changing a thing.. very strange
regards

Avatar
camfindlay

Forum Moderator, 171 Posts

30 December 2013 at 12:17pm

glad to hear it is working, if you manage to work out why it occurred it might be valuable to post that here in case others have similar problems in there projects.