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.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

My Gallery Module Preview Code

Go to End

3 Posts   1840 Views

Yellow7 Jon

Community Member, 39 Posts

26 June 2008 at 4:32am

So I'm still pretty new to silverstripe (about 3 or 4 days), so this may be covered elsewhere. If it is, oh well, I still learned a good bit about how the pages interact.

In my on going quest to find a gallery preview script, one that would display X number of images out of a gallery and link to it, I just opted to create my own.

On with the code!

In the GalleryPage.php insert

static $db

		'PreviewLimit' => 'Int',
		'PreviewWidth' => 'Int',
		'PreviewHeight' => 'Int',
		'PreviewCols' => 'Int'

static $defaults --- You can use what ever defaults fit your needs best.

		'PreviewLimit' => 4,
		'PreviewWidth' => 75,
		'PreviewHeight' => 75,
		'PreviewCols' => 4

RUN /db/build?flush=1

in function getCMSFields( $cms ) --- this will add a new tab on the gallery page to edit the preview settings.

		$fields->addFieldsToTab( 'Root.Content.Gallery Preview', array( 
			new HeaderField( _t('GalleryPage.GALLERYPREVIEWLAYOUT','Child Gallery Preview Layout') ),
			new NumericField( 'PreviewLimit', _t('GalleryPage.GALLERYPREVIEWLIMIT','Child Gallery Preview Limit') ),
			new NumericField( 'PreviewWidth', _t('GalleryPage.GALLERYPREVIEWWIDTH','Child Gallery Preview Image Width') ),
			new NumericField( 'PreviewHeight', _t('GalleryPage.GALLERYPREVIEWHEIGHT','Child GalleryPreview Image Height') ),
			new NumericField( 'PreviewCols', _t('GalleryPage.GALLERYPREVIEWCOLS','Child Preview Images Per Row') )

INSERT THIS FUNCTION into the GalleryPage class

	function GalleryPreview () { 
		$parent = $this->getParent(); // Get the data from the page you are on.
		if ( $parent->class != "GalleryPage" ) // if for some reason the page you are on is not a gallery, use the child gallery info.
			$parent = $this; 
		$items = $this->GalleryItems( $parent->PreviewLimit ); // get the gallery
		$output = "<table class=\"gallery_preview\"><tr>";
		$x = 0; // column counter
		foreach ( $items AS $item )
			// Taken for word for word from GalleryItems... saw no need to add code to GalleryItems if this would only be used in a preview
			$ext = $item->getExtension();
			if( ! is_bool( strpos( GalleryPage_Extension::$exts[ 'Images' ][ 'Extensions' ], $ext ) ) ) { // ...create/Get the formatted image...
				$imgForThumbnail = $item->newClassInstance( "GalleryPage_Image" );
			if( method_exists( $imgForThumbnail, "generate{$this->ThumbnailType}" ) )
				$imgThumbnail = $imgForThumbnail->getFormattedImage( $this->ThumbnailType, $parent->PreviewWidth, $parent->PreviewHeight );
				// Uses $this->ThumbnailType instead of $parent->ThumbnailType to get the same type of thumbnail that will be displayed in the gallery
			else // Fall back to the de facto image resize method
				$imgThumbnail = $imgForThumbnail->getFormattedImage( 'ResizedImage', $parent->PreviewWidth, $tparent->PreviewHeight );
			// New row
			if ( !($x % $parent->PreviewCols) )
				$output .= "</tr><tr>";
			$output .= "<td width=\"".round(100 / $parent->PreviewCols)."%\"><img src=\"".Director::baseURL().$imgThumbnail->Filename."\" /></td>";
		$output .= "</tr></table>";
		return $output;


<% control Children %>
	<div class="gallery_preview"><a href="$Link">$Title</a><br />
<% end_control %>

Hope this helps some new or even experienced users out!


Forum Moderator, 5514 Posts

26 June 2008 at 2:08pm

Useful code snippets work well in the documentation! You might like to make a page like 'Gallery Modifications' or something and link to it from the modules:gallery

Yellow7 Jon

Community Member, 39 Posts

28 June 2008 at 5:35am

i may just do that! thanks for the feed back