Jump to:

3450 Posts in 1063 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Gridfield Image Class display Thumbnail [solved]

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

Page: 1
Go to End
Author Topic: 441 Views
  • Matty Balaam
    Avatar
    Community Member
    69 Posts

    Gridfield Image Class display Thumbnail [solved] Link to this post

    I’m upgrading a site from SS2 to SS3 and there are a number of Images which are managed by ImageDOM which I am trying to move to Gridfield.

    Usually I will have this:

    class CustomImage extends DataObject {

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

    public function getThumbnail() {
    return $this->MyImage()->CMSThumbnail();
    }
    }

    Then if I create a gridfield Record Editor on a page this automatically gives me a thumbnail.

    class Page extends SiteTree {

    ...

    public static $has_many = array(
    'CustomImages' => 'CustomImage',
    );

    public function getGeneratedCMSFields() {
    $fields = parent::getCMSFields();

    $conf=GridFieldConfig_RecordEditor::create(10);
    $fields->addFieldToTab("Root.CustomImages", new GridField('CustomImages','', $this->CustomImages(), $conf));

    return $fields;
    }

    ...
    }

    Is it possible to adapt this to work on the basic Image class? So far I have tried this, wihch I feel may be close, but I can’t work out how to get it to work.

    class Page extends SiteTree {

    ...

    $has_many = array(
    'MyImages' => 'Image'
    )

    ...

    public function getThumbnail() {
    return $this->Image()->CMSThumbnail();
    }   

    public function getCMSFields() {
    $fields = parent::getCMSFields();

    $conf=GridFieldConfig_RecordEditor::create(10);
    $conf->getComponentByType('GridFieldDataColumns')
    ->setDisplayFields(array(
    'Thumbnail' => 'Thumbnail'      
    'Title' => 'Title'
    )) ;            
    $fields->addFieldToTab("Root.MyImages", new GridField('MyImages','', $this->MyImages(), $conf));
    return $fields;
    }

    Is it possible to display a thumbnail, or should I just re-organise the site structure?

  • Matty Balaam
    Avatar
    Community Member
    69 Posts

    Re: Gridfield Image Class display Thumbnail [solved] Link to this post

    After getting away from the screen for a few minutes and coming back I solved it almost straight away:

       public function getThumbnail() {
        return $Image()->CMSThumbnail();
       }   

    ...

       ->setDisplayFields(array(
          'Image' => 'CMSThumbnail',         
          'Title' => 'Title'
       ))       
       ;   

    441 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.