SSbits - Home page
Site by Carbon Crayon
Submit a Post >

Snippets - Little bits of code to make you happy

Adding a Thumbnail to a DataObjectManager

SSBDOMtumb

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'
);
Aram Balakjian avatar

Aram Balakjian

Aram is a web developer running London based agency Aab Web. He has a strong passion for developing attractive, usable sites around the SilverStripe CMS.

  • web2works
    07/02/2011 7:31am (4 years 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 (4 years 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 (4 years ago)

    Thanks Bart, that worked a treat

  • Darren-Lee
    09/04/2011 1:27pm (4 years 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 (3 years 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 (3 years ago)

    This is a great technique, also useful to display Money fields in a complex table field.

  • Adam Stead
    02/08/2012 3:16am (2 years ago)

    Something i came accross when using this technique with model admin you will need to specify static $searchable_fields as with out it when pointing to a function the scoffolding chucks a fit. :)

    Besides that works so simply.

  • Blair
    05/12/2013 11:15pm (11 months ago)

    Hi Aram,

    Thanks for the great content you've been posting here on SSbits, am definitely finding some useful articles on here.

    Am having some trouble with the thumbnail on the DataObjectManager:

    I am working on integrating this into a ManyManyDataObjectManager.

    Note: this is using the SS Shop module 0.8.5 in SS 2.4.7 between the "Product" and "ProductCategory" classes.

    I have the getThumbnail function in my product class.
    Unfortunately adding the thumbnail call to either the $summary_fields array (in the product class), or directly to the DOM definition (in the ProductCategory class) causes an error.

    Adding to the $summary_fields causes a large Query error:
    "
    [User Error] Couldn't run query: SELECT "SiteTree"."ClassName", .........
    "

    Adding to the DOM definition itself results in the white screen of death.

    Any clues???
    Thank you,

Post a comment ...

You cannot post comments until you have logged in. Login Here.

Advertisement

Site of the Month

Find SSbits on

Top Contributers

Rank Avatar Name
1 article image Aram Balakjian
2 article image Daniel Hensby
3 article image Marcus Dalgren
4 article image Hamish Campbell
5 article image Ty Barho
6 article image Martijn van Nieuwenhoven
7 article image Darren-Lee
8 article image Roman Schmid
9 article image Matt Clegg
10 article image dalesaurus

View full leaderboard


Advertisement