I'm working on a project where each page on the site needs to have anywhere from one to many images attached to it. These are not images which exist within the page's content, but rather separate images which I'll need to manipulate into things like thumbnails, etc..
Having followed the tutorials, along with a little bit of guess work, I've managed, using the following code, to get something working:
/mysite/code/Page.php
class Page extends SiteTree
{
public static $db = array();
public static $has_one = array();
public static $has_many = array(
'Photos' => 'Photo'
);
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Images', new UploadField('Photos'));
return $fields;
}
}
/**
* Allows the upload of multiple images to pages
* http://www.silverstripe.org/general-questions/show/20345#post315482
*/
class Photo extends Image
{
public static $has_one = array(
'Page' => 'Page'
);
}
As this is the base Page class, which all my other extend, it means with very little effort I've been able to add image uploading to every page on the site, accessing them in the theme files like so (for example)..
<% if $Photos %>
<% loop $Photos %>
$CroppedImage(630,320)
<% end_loop %>
<% end_if %>
This is fine, so far, but it has a couple of problems with which I'm looking for some help.
The first issue is that I'm only able to have each image attached to one page, where as I was hoping for a more flexible system which would let me attach the same image to multiple pages. I realise that I could just upload multiple copies of the same image, but that isn't really fixing the problem, it's just bypassing it.
Second, I'm looking for a way in which I'm able to order the images, so that when I do a loop (like above), the image I assigned as first in the CMS is the first in the loop.
Can anyone help point me in the right direction on how I'm able to fix these issues?
Thank you :)