The most basic gallery can be made using two custom page types:
GalleryHolder
GalleryPage
You first create a Gallery Holder in SilverStripe which can be used to show all photos as a landing page (if desired), then under the Gallery Holder you create Gallery Pages which are the individual gallery items.
Example:
GalleryHolder.php:
===============
<?php
class GalleryHolder extends Page {
// gallery pages are the default page to be created under a holder
static $default_child = "GalleryPage";
// only gallery pages are allowed under a holder section
static $allowed_children = array("GalleryPage");
/**
* Return the gallery pages with a limit you can set in template
* default is 5 if nothing set
*/
function GalleryItems($limit = 5) {
return DataObject::get("GalleryPage","`ParentID` = '{$this->ID}'", "", "", $limit);
}
}
GalleryPage.php:
==============
<?php
class GalleryPage extends Page {
// define your database fields here - for example we have author
static $db = array(
"Author" => "Varchar(100)"
);
// define image class relationships here (see Photo_GalleryImage class below)
static $has_one = array(
"Image" => "Photo_GalleryImage",
);
function getCMSFields($cms) {
$fields = parent::getCMSFields($cms);
$fields->addFieldToTab("Root.Content.Main", new TextField("Author","Photo author"));
$fields->addFieldToTab("Root.Content.Main", new ImageField("Image", "Photo"));
return $fields;
}
}
class GalleryPage_Image extends Image {
// define different GD functions to resize uploaded photos, see the GD page in the SilverStripe documentation for different functions for resampling
function generateThumbnail($gd) {
$gd->setQuality(90);
return $gd->resizeByWidth(288);
}
function generateNormal($gd) {
$gd->setQuality(90);
return $gd->resizeByWidth(395);
}
}
?>
You can then define some templates for these page types:
GalleryHolder.ss:
=============
<div id="Gallery">
<% if GalleryItems %>
<ul class="galleryList">
<!-- define limit in brackets -->
<% control GalleryItems(10) %>
<li>
<h3>$Title</h3>
<img src="$Image.Thumbnail.URL" alt="$Title photo" />
<p class="details">Added on $Created.Nice<% if Author %> by $Author<% end_if %></p>
</li>
<% end_control %>
</ul>
<% end_if %>
</div>
GalleryPage.ss:
============
<div id="Content">
<h2>$Title</h2>
<img src="$Image.Thumbnail.URL" alt="$Title photo" />
<p class="details">Added on $Created.Nice<% if Author %> by $Author<% end_if %></p>
</div>
You can get even fancier with pagination - ie. a results block with << Prev 1 2 3 Next >> etcetera, but this would be a good starting point if you're interested in making a gallery.