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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

[Solved] Image Thumbnails in Gridfield


Go to End
Reply


24 Posts   10432 Views

Avatar
UndefinedOffset

Community Member, 30 Posts

5 July 2012 at 4:21am

Doesn't look to be, the last commit to the effected file was a month ago.

Avatar
jak

Community Member, 46 Posts

30 July 2012 at 12:07am

This will be fixed in 3.0.1 (currently RC2).

Avatar
JonShutt

Community Member, 244 Posts

10 October 2012 at 5:19pm

seems that in silverstripe 3.0.2 it works if you use

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

i just made a quick tutorial about it here too:
http://www.silverstriperesources.com/articles/silverstripe-3-0-2-grid-fields-with-thumbnails/

Avatar
neilcreagh

Community Member, 115 Posts

11 October 2012 at 12:58am

Thank you!

Avatar
priithansen

Community Member, 25 Posts

19 October 2012 at 11:45am

For some reason the Futuraweb solution doesn't seem to work with ModelAdmins gridfield.
Have to use this nasty thing

<?php
class SliderImage extends DataObject {

	static $db = array(
		'Description' => 'Varchar(255)'
		);

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

	static $summary_fields = array(
		'ImageNice' => 'CustomPicTitle',
		'DescriptionNice' => 'CustomDescTitle'
		);

	static $casting = array(
		'ImageNice' => 'HTMLText',
		'DescriptionNice' => 'Varchar'
	);
	
	public function getImageNice() {
		return $this->Image()->CMSThumbnail();
	}
	public function getDescriptionNice() {
		return $this->Description;
	}
}

Otherwise I get an error "Unable to traverse to related object field [Thumbnail] on [SliderImage]"
And using the casting for Description is the only way the gridfield columns pick up my custom titles aswell.

Avatar
arnhoe

Community Member, 6 Posts

14 December 2012 at 1:33am

And using the casting for Description is the only way the gridfield columns pick up my custom titles aswell.

You can no longer change field names through $summary_fields. You should use $field_labels, "Title" => "Titel"

Avatar
RobC

Community Member, 26 Posts

29 April 2013 at 4:35pm

I've just fixed the problem when trying to add thumbnails to model admin in SS3

If you're getting "Can't traverse to [image something] on your object"

then you just need to add searchable fields, and it fixes it!

Something like this: (use your field names obviously)

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

Rob Clarkson

Avatar
jmp909

Community Member, 5 Posts

16 January 2016 at 4:52pm

Edited: 16/01/2016 5:52pm

I'm having an issue with this also, using the following code, it looks like the 'get' Function is referring to the field alias, not the field name

<?php

class Speaker extends DataObject {
    
    private static $db = array(
        'Name' => 'Varchar',
        'Title' => 'Varchar',
        'Company' => 'Varchar',
    );

    private static $has_one = array(
    	'Photo' => 'Image'
    );

    private static $summary_fields = array(
        'Name',
        'Title',
        'Company',
        'Thumbnail' => 'SpeakerPhoto' // but i want this to be 'Speaker Photo' as the label
    );
                            ||
                            ||
                           \  /
                            \/
                                              
    // this should ideally be getThumbnail
    public function getSpeakerPhoto() { // can't use get with 'Speaker Photo' 
       if ($Image = $this->Photo()->ID) { 
			return $this->Photo()->SetWidth(80); 
		} else { 
			return '(No Image)'; 
		}
	} 
}

thanks for any advice
J

UPDATE:

it turns out all fields would need to be aliased in this case, in order for the get function to refer to the field name, not the field alias

this works now..

private static $summary_fields = array(
    'Name' => 'Name',
    'Title' => 'Title',
    'Company' => 'Company',
    'Thumbnail' => 'Speaker Photo'
);

public function getThumbnail() { // references the field name above, not the alias
    if ($this->Photo()->exists()) { 
        return $this->Photo()->SetWidth(80); 
    } else { 
        return '(No Image)'; 
    }
}

Go to Top