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.

Data Model Questions /

Gridfield Image Class display Thumbnail [solved]


Reply


2 Posts   514 Views

Avatar
Matty Balaam

Community Member, 69 Posts

10 February 2013 at 6:33am

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?

Avatar
Matty Balaam

Community Member, 69 Posts

10 February 2013 at 6:58am

Edited: 10/02/2013 6:58am

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