Snippets - Little bits of code to make you happy
Adding a Thumbnail to a DataObjectManager
Tweet21 May 2009 | | | Supports v2.4, v2.3
This snippet lets you add a thumbnail to items in UncleCheese's Data Object Manager module or a regular Complex Table field. All you need to do is create a function that returns a thumbnail and refer to this function in your DOM or CTF definition.
So add a function like this to your DataObject class:
function getThumbnail()
{
if ($Image = $this->Image())
{
return $Image->CMSThumbnail();
}
else
{
return '(No Image)';
}
}
Notice the use of CMSThumbnail() SilverStripe has already done the hard work of creating the thumbnail so all you need to do is call this function on your image!
Then in your same DataObject class, you simply add the Thumbnail call to your $summary_fields static. You could also call it in the actual DOM definition if you wanted to.
static $summary_fields = array( 'Thumbnail' => 'The Image' );
6 Comments
RSS feed for comments on this page RSS feed for all comments
web2works
07/02/2011 7:31am (12 months ago)
I am having a problem getting this to work.
I have put the getThumbnail() into the Many relationship class containing the image. Then the $summary_fields in the other end of the relationship.
Anything I change or try makes no difference to the images being resized.
Thanks Ben
Bart van Irsel
15/02/2011 10:17am (12 months ago)
Hi,
Maybe this helps, HeaderImage.CMSThumbnail.Tag worked for me:
static $has_one = array(
"HeaderImage" => 'HeaderImage',
);
//Fields to show in the DOM table
static $summary_fields = array(
'HeaderImage.CMSThumbnail.Tag' => 'Header Image Label'
);
public function getCMSFields() {
$fields = new FieldSet(
new ImageField('HeaderImage', 'HeaderImage', Null, Null, Null, 'Uploads/website-headers/')
);
return $fields;
}
tomg
16/03/2011 5:32pm (11 months ago)
Thanks Bart, that worked a treat
Darren-Lee
09/04/2011 1:27pm (10 months ago)
Great tip! I've found this very useful for creating a friendly admin interface summary table for my clients to manage certain types of image sliders.
MRKDevelopment
01/06/2011 7:17am (8 months ago)
Used this today, very helpful. Any chance of showing us how to use the dataobject manager to control an image library ? Ive noticed that you have used it on a project with aab web.
Thanks,
Frank Mullenger
05/10/2011 6:27am (4 months ago)
This is a great technique, also useful to display Money fields in a complex table field.
Post a comment ...
You cannot post comments until you have logged in. Login Here.