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

Problem adding mutiple images to page


Go to End
Reply

13 Posts   2202 Views

Avatar
Pix

23 July 2012 at 8:28am (Last edited: 23 July 2012 8:30am), Community Member, 145 Posts

Hi All,

I have a custom page with multiple images, and everything is working fine. It's built like this right now:

static $has_one = array(
'Photo' => 'Image',
'PhotoTwo' => 'Image',
'PhotoThree' => 'Image',
'PhotoFour' => 'Image',
'PhotoFive' => 'Image',
'SmallBanner' => 'Image'
);

The problem comes when I try to add just one more image, it blows up the site and I get "Sorry, there was a problem handling your request". I am using SS 2.4.5. Is there some sort of limit? Or a better way to do it? Or am I just missing something (besides brain cells)?

Thanks

Avatar
Hattori

24 July 2012 at 9:46am Community Member, 20 Posts

Has a better way, use Data Object Manager

http://www.silverstripe.org/dataobjectmanager-module/
http://doc.silverstripe.org/old/modules:dataobjectmanager

Avatar
svandragt

25 July 2012 at 12:41am Community Member, 44 Posts

Data Object Manager is not yet compatible with SilverStripe 3?

[User Deprecated] DataObjectDecorator->__construct is deprecated. Use DataExtension instead. Called from ReflectionClass->newInstance.

Avatar
svandragt

25 July 2012 at 1:01am (Last edited: 25 July 2012 1:08am), Community Member, 44 Posts

I just got this working, you do not need to install any module anymore in SilverStripe 3.0:

Because the [url=http://api.silverstripe.org/3.0/framework/forms/UploadField.html]UploadField can now handle multiple files/images[/url], we simply specify a has_many relationship and create a screenshot class that extends the [url=http://api.silverstripe.org/2.4/sapphire/filesystem/Image.html]Image DataObject[/url]. Remember to set a link back from the dataobject to the page type:

In
mysite/code/YourPage.php:

class YourPage extends Page {

   public static $has_many = array(
      'Screenshots' => 'Screenshot'
   );

   public function getCMSFields() {
      $fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main', new UploadField('Screenshots'), 'Content');
      return $fields;
   }
}

class Screenshot extends Image {
      public static $has_one = array(
         'YourPage' => 'YourPage'
   );   
}

Then, in your theme's
templates/Layout/YourPage.ss, we can just call the has_many relationship and use all the Image class's methods:

<div class="content-container typography">   
   <article>
      <h1>$Title</h1>
      <div class="content">
            <% loop Screenshots %>
               $SetWidth(320)
            <% end_loop %>
         $Content
      </div>
   </article>
      $Form
      $PageComments
</div>
<% include SideBar %>

Avatar
Pix

25 July 2012 at 5:47am Community Member, 145 Posts

Hi,

Thanks for all the replies. That looks like a good solution. Haven't upgraded to 3.0 yet, I guess I better do that

For now I just can't see why the current code is bombing....is there a quick and easy fix?

Thanks

Avatar
MartinPhone

15 November 2012 at 1:47pm Community Member, 57 Posts

Thanks for this. Still trying to get my head around what SS3 can and can't do and this example was just what I needed.

Avatar
congii

31 January 2013 at 5:37pm Community Member, 6 Posts

Hi, this works on the CMS side, in the front-end form I can't seem to make this work using FileField.

Avatar
mimamo

8 February 2013 at 12:02am Community Member, 22 Posts

can someone tell me how to add a variable to get the images instead of $SetWidth ?

Go to Top