Jump to:

3431 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Show thumbnail in gridfield

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

Page: 1
Go to End
Author Topic: 2099 Views
  • sashion
    Avatar
    Community Member
    23 Posts

    Show thumbnail in gridfield Link to this post

    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

  • SparkGreen
    Avatar
    Community Member
    5 Posts

    Re: Show thumbnail in gridfield Link to this post

    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'
    );

  • sashion
    Avatar
    Community Member
    23 Posts

    Re: Show thumbnail in gridfield Link to this post

    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

  • elgordo
    Avatar
    Community Member
    70 Posts

    Re: Show thumbnail in gridfield Link to this post

    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

  • vwd
    Avatar
    Community Member
    158 Posts

    Re: Show thumbnail in gridfield Link to this post

    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.

  • ocean
    Avatar
    Community Member
    37 Posts

    Re: Show thumbnail in gridfield Link to this post

    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!

  • ocean
    Avatar
    Community Member
    37 Posts

    Re: Show thumbnail in gridfield Link to this post

    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

    2099 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.