Jump to:

23490 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Need Help - Uploading and Displaying Multiple Images

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: 304 Views
  • BarcelonaV
    Avatar
    Community Member
    4 Posts

    Need Help - Uploading and Displaying Multiple Images Link to this post

    Hello, I'm very new to SilverStripe and like the title says, I'm trying to upload multiple images at once and then be able to display them through my template. I haven't been able to get it to work how I want it to. I'm using SilverStripe 3.

    The code for my ImagePage.php file is below where I create the form in the CMS, everything uploads to the "Images" folder I created within the assets folder just fine.

    <?php

    class ImagePage extends Page {

    static $has_many = array(
    "PageImages" => "Image"
    );

    public function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Main', $uploadField = new UploadField('PageImages'));
    $fields->removeFieldFromTab('Root.Main', 'Content');

    $uploadField->setFolderName('Images');

    return $fields;
    }

    }

    class ImagePage_Controller extends Page_Controller {

    }

    ?>

    The code for my template is as follows:

    <% if PageImages %>
    <% loop PageImages %>
    <img src="$URL" alt="$Title" />
    <% end_loop %>
    <% end_if %>

    The problem is that when I go to view the page that should be displaying the images, they don't display and instead have the "non existant/broken link" image in its place. When I inspect the element it shows that the url being supplied by $URL is '/silverstripe/assets/image1.jpg instead of what I want, '/silverstripe/assets/images/image1.jpg'.

    I can't seem to find anything that simply explains how to display multiple images from within a specific folder. I would greatly appreciate it if someone could explain the steps necessary to making this work for me.

  • BarcelonaV
    Avatar
    Community Member
    4 Posts

    Re: Need Help - Uploading and Displaying Multiple Images Link to this post

    Resolved my issue.

    After hours of Google searches and forum digging, turns out that the segment:

    "static $has_many = array(

    "PageImages" => "Image"

    );"

    should be:

    "static $many_many = array(

    "PageImages" => "Image"

    );"

    Hopefully this will save other newbies the trouble I went through!

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