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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Show thumbnail in gridfield


Go to End


7 Posts   8784 Views

Avatar
sashion

Community Member, 25 Posts

28 August 2012 at 9:47pm

Hey Folks,

is it possible to show a thumbnail in the gridfield in SS3?

I aready tried this but I only get an additional blank column:

  // Summary fields
   public static $summary_fields = array(
   		'ID' => 'Pic ID',
   		'Picname' => 'Name of the Picture',
   		'Image.StripThumbnail' => 'Picture'
   );

I´m using version 3.0.1

Thx in advance
Sash

Avatar
SparkGreen

Community Member, 6 Posts

29 August 2012 at 11:06am

I had a solution in 2.4 - should work in 3.01 as well.

The problem was instead of defining the Image in $db I had to define it as a $has_one, and create a function to return in the summary_fields. Try this:


static $has_one = array(
	'MyImage' => 'Image',
);

function MyImageThumb(){ 
	return $this->MyImage()->SetHeight(40);
}

public static $summary_fields = array( 
      'ID' => 'Pic ID', 
      'Picname' => 'Name of the Picture', 
      'MyImageThumb' => 'Picture' 
);

Avatar
sashion

Community Member, 25 Posts

31 August 2012 at 9:34pm

Edited: 01/09/2012 2:53am

Hey SparkGreen,

thanks for your answer. I already tried the way you proposed but it ends up with an Error: "Unable to traverse to related object field [MyImageThumb] on [Person]"

EDIT: Ok, finally I got the solution. So hopefully this will help someone else save some time.

First of all both variants (the one by SparkGreen and mine) work. I think in SS3 it´s even a bit easier to do it my way. But both solutions are fine. The important thing for me to get it work was that I had did have to define the searchable fields. If you don´t do it manually you get an error (like the one above).

So this is the working solution to me:

Defining the summary fields like this:

public static $summary_fields = array(
    		'MyImage.StripThumbnail' => 'Picture',
    		'ID' => 'Pic ID',
    		'Name' => 'Name of the Picture'
);

...and the important thing not to forget define the searchable fields manually like this:

static $searchable_fields = array(
    'ID',    		
   'Name'
);

Cheers,
sash

Avatar
elgordo

Community Member, 70 Posts

3 December 2012 at 5:37pm

Hat tip to Simon Welsh for this. I was confronted with a slightly different situation where I had the URL already, from the Flickr API. Do the following:

public function getThumbnail() { 
      return DBField::create_field('HTMLVarchar', '<img src="'.$this->ThumbnailURL.'"/>');
}

One needs to return an object. If a String is returned it gets escaped, rendering the HTML in the Grid Field.

It looks like this method could be used to render arbitrary HTML, though HTMLVarchar is limited to 255 characters. I guess HTMLText could be used for longer, but then why would you want to render a large amount of HTML in what is essentiall a summary field.

Cheers

Gordon

Avatar
vwd

Community Member, 166 Posts

18 January 2013 at 4:21am

Hi,

Was looking to do the same thing. However when trying it out on SS 3.0.3, I didn't need explicitly set $searchable_fields. Just adding the image thumbnail to $summary_fields was sufficient.

I.e. only

public static $summary_fields = array( 
      'MyImage.StripThumbnail' => 'Image', 
      'Title' => 'Title' 
);

VWD.

Avatar
ocean

Community Member, 37 Posts

25 February 2013 at 1:42am

Hello

I've encountered a similar problem which neither of these solutions seem to solve for me (SS 3.0.3), any timely guidance much appreciated...

this is the error when viewing the StoreImage tab in CMS: [User Error] Uncaught Exception: Unable to traverse to related object field [ImageThumbnail] on [StoreImage]
Be aware, I'm noting particularly the notice 'user error' here : )

Further, its working perfectly when called from the related Product DataObject, ie, the thumbnail is showing in the Product Tab section

this is the setup:

Both of the following DataObjects are setup as Managed Models as defined in: class ProductAdmin extends ModelAdmin

StoreImage.php

class StoreImage extends DataObject {
  
    public static $db = array(	
            'SortOrder' => 'Int',
            'Title' => 'Varchar',
            'Type' => "Enum('Product Image, Producer Image, Category Image')"
    );
 
    // One-to-one relationship with product object
    public static $has_one = array(
            'Image' => 'Image',
            'Product' => 'Product'	
    );
  

    // tidy up the CMS by not showing these fields
     public function getCMSFields() {
            $fields = parent::getCMSFields();
            $fields->removeByName('SortOrder');
            $fields->removeByName('ProductID');
            return $fields;		
     }

    // this function creates the thumbnail for the summary fields to use
    public function ImageThumbnail() { 
            return $this->Image()->SetHeight(50);
    }
     
    // Tell the datagrid what fields to show in the table
     public static $summary_fields = array( 
            'Title' => 'Title',
            'Type' => 'Type',
            'ImageThumbnail' => 'Thumbnail'
     );

Product.php

class Product extends DataObject {
        static $db = array('Name' => 'Varchar', 'Description' => 'Text','ProductCode' => 'Varchar', 'Price' => 'Currency');
        static $has_one = array('ProductCategory' => 'ProductCategory', 'StoreImage' => 'StoreImage', 'ProductProducer' => 'ProductProducer', 'ProductPage' => 'ProductPage');
        
        // For search in CMS
        static $searchable_fields = array(
           'Name',
           'ProductCode'
        );
        static $summary_fields = array(
           'Name' => 'Name',
           'StoreImage.ImageThumbnail' => 'Product Image',
           'Description' => 'Description',
           'Price' => 'Price'
           
        );

Thanks for looking!

Avatar
ocean

Community Member, 37 Posts

25 February 2013 at 3:11am

Ok, for the record the $searchable_fields option fixed it...

I did not really want the search feature in this case but its better than an error!!! :)

StoreImage.php

Added:

    static $searchable_fields = array(
            'Title',
            'Type'
    );

Best wishes
~ Sean