SilverStripe 3.1 Gallery Using Gridfield

In this post I will tell you how to make a working gridfield gallery for Silverstripe CMS 3.1.

Modules needed: SortableGridField

  1. Create Page called Gallery.php inside folder mysite/code/:
    class Gallery extends Page {
       public static $has_many = array(
        	'GalleryImages' => 'GalleryImage'
       public function getCMSFields() {
       	$fields = parent::getCMSFields();  
    	$gridFieldConfig = GridFieldConfig_RecordEditor::create();  
    	$gridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
    	$gridfield = new GridField("GalleryImages", "Gallery Images", $this->GalleryImages()->sort("SortOrder"), $gridFieldConfig);
    	$fields->addFieldToTab('Root.Content.Main', $gridfield);	
    	return $fields;		
    class Gallery_Controller extends Page_Controller {
    	public static $allowed_actions = array (
    	public function Photos() {
    		return $this->GalleryImages()->sort("SortOrder");
    	public function init() {
  2. Also add file named GalleryImage.php to mysite/code/ folder:
    class GalleryImage extends DataObject {
      public static $db = array(	
    	  'SortOrder' => 'Int',
    	  'Title' => 'Varchar'
      public static $has_one = array(
        'Image' => 'Image',
        'Gallery' => 'Gallery'	
      public function getCMSFields() {
     	$fields = parent::getCMSFields();
    	return $fields;		
       public static $summary_fields = array( 
    	'ID' => 'ID',
    	'Title' => 'Title',
    	'Thumbnail' => 'Thumbnail'     
       public function getThumbnail() { 
         return $this->Image()->CMSThumbnail();
  3. In themes/yourtheme/templates/Layout/ folder make file named with code:
    <% control Photos %>
    <% control Image %>
    // In here you can add any stuff like rel="" for ajax 
    <a href="$SetHeight(200).URL">$SetWidth(500)</a>
    <% end_control %>
    <% end_control %>
  4. Rebuild database using
  5. also log out from admin and login again and then start making your gallerypage by adding Gallery

Looking for quality SilverStripe Web Hosting? Look no further than Arvixe Web Hosting !

Tags: , , , , , , , , | Posted under SilverStripe CMS | RSS 2.0

Author Spotlight

Teet Bergmann

Teet Bergmann

I'm a freelance web designer from Estonia. Mostly doing sites using Silverstripe, magento, prestashop, wordpress.

2 Comments on SilverStripe 3.1 Gallery Using Gridfield

  1. Steve says:

    I found this very useful, thank-you! One question though, how do I adjust the upload file size and extensions?


    • for Size use in php.ini:
      php_value memory_limit 128M
      file_uploads = On
      upload_max_filesize = 100M

      And for extensions go to assets folder open the .htaccess and add you extensions to FilesMatch

Leave a Reply

Your email address will not be published. Required fields are marked *

9 − = 0

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>