Jump to:

22975 Posts in 11620 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Showing images from UploadField in custom template

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

Page: 1
Go to End
Author Topic: 187 Views
  • Martinjj
    Avatar
    Community Member
    12 Posts

    Showing images from UploadField in custom template Link to this post

    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

  • camfindlay
    Avatar
    Forum Moderator
    74 Posts

    Re: Showing images from UploadField in custom template Link to this post

    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.

  • Martinjj
    Avatar
    Community Member
    12 Posts

    Re: Showing images from UploadField in custom template Link to this post

    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

  • camfindlay
    Avatar
    Forum Moderator
    74 Posts

    Re: Showing images from UploadField in custom template Link to this post

    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.

    187 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.