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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

Adding multiple images to a page type


Go to End


12 Posts   11478 Views

Avatar
MarioSommereder

Community Member, 107 Posts

28 January 2010 at 6:51am

Hi,

I'm trying to add a news section where each article can have multiple images. Therefore I want to have a tab "Photos" where the editor can upload images through the backend (like ImageField), as many as he wants. There is no need to also be able to browse the existing files.

Can anyone give me a hint, how to start? Big thanks in advance!

Cheers, Mario

Avatar
zenmonkey

Community Member, 545 Posts

29 January 2010 at 7:18am

Unfortunately the Image Upload in the HTML editor field has been removed so the user would have to upload images from the Files and Images interface and place it into the Content field with add image function and select from dropdown.

If the Image position is determined by the template and the Content field you could always add an ImageDataObjectManger from DataObjectManager Module.

Avatar
Jatinder

Community Member, 23 Posts

4 February 2010 at 5:33am

Take a look at http://www.silverstripe.org/archive/show/7715

This should all the info you need to attach multiple images to page type.

Avatar
Ben_W

Community Member, 80 Posts

19 February 2010 at 7:05pm

in your page type class add in following

static $has_many = array (
'GalleryImages' => 'GalleryImage'
);

function getCMSFields() {
$fields = parent::getCMSFields();
...

$manager = new ImageDataObjectManager(
$this, // Controller
'GalleryImages', // Source name
'GalleryImage', // Source class
'MyGalleryImage', // File name on DataObject
array(
'GalleryImageTitle' => 'GalleryImageTitle'
), // Headings
'getCMSFields_forPopup' // Detail fields (function name or FieldSet object)
// Filter clause
// Sort clause
// Join clause
);
$fields->addFieldToTab('Root.Content.GalleryImage',$manager);

...

return $fields;
}

add the following dataobject in a separate file

class GalleryImage extends DataObject
{
static $db = array (
'GalleryImageTitle' => 'Text'
);
static $has_one = array (
'MyGalleryImage' => 'Image', //relation needed for this DataObject
'BelongToEventPage' => 'EventPage' //relation needed to point back to your pagetype, my pagetype is EventPage.
);

public function getCMSFields_forPopup()
{
return new FieldSet(
new TextField('GalleryImageTitle'),
new FileIFrameField('MyGalleryImage')
);
}

}

You would also need the add in following two module 'DataObjectManager', 'SWFUpload', remember to do a /dev/build?flush=1 after you upload this two module onto your server.

then in your template do following
<% if GalleryImages %>
<% control GalleryImages %>
<img src="$MyGalleryImage.URL" width="90" />
<% end_control %>
<% end_if %>

Avatar
MarioSommereder

Community Member, 107 Posts

19 February 2010 at 10:31pm

I like that one! Thank you for that!

Avatar
jovoo

Community Member, 12 Posts

27 February 2010 at 2:45am

And I like it, too. Exactly what I needed!
Thank u, Ben_W!

Avatar
BigChris

Community Member, 63 Posts

2 March 2010 at 6:35am

Thank you Ben_W.

Excellent stuff. Can I ask though how you would add Thumbnails?

I am not sure how to add it, but managed to get the template to render it on the front end by using

$MyGalleryImage.CMSThumbnail

This creates/displays a thumbnail but is cannot be resized, as I would like to have a thumbnail at 70 by 70.

Does anyone know how to add it to the Class or a template call I can use?
(I tried various other GD function but I dont know enough to get it to work.)

Also if you want to be able to re-order the Gallery you can add
SortableDataObject::add_sortable_class('GalleryImage');
to the mysite/_config.php

Cheers
Chris

Avatar
Ben_W

Community Member, 80 Posts

2 March 2010 at 11:49am

Have a look at this page: http://doc.silverstripe.org/doku.php?id=image
The easiest way in your case would be use $MyGalleryImage.SetSize(70,70)
Please note this will add necessary white padding, ie if after resize the viewable image is 70x60, the it will add 5px white padding on both end vertically, so that your final image will be 70x70.

If you need to customise image, then have a look at the following pages:
http://doc.silverstripe.org/doku.php?id=recipes:customising_image&s=custom%20image
http://doc.silverstripe.org/doku.php?id=recipes:gallery&s=custom%20image

Go to Top