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.

Form Questions

Images in TableListField


Reply

4 Posts   1041 Views

Avatar
zenmonkey

21 January 2011 at 9:59am Community Member, 528 Posts

I have a table list field in the front end linked to a dataobject with has_one relationships to images. Including the IMage relations calls the full image, but I'd like to use a resized image. I have a custom getter on the DataObject that works in MOdel admin but it won't work in the front end.

Avatar
omarkohl

26 February 2011 at 4:08am Community Member, 30 Posts

I have exactly the same problem. I want to generate a report (which uses TableListField) and inside the report I have some huge images I want to resize before showing.
I have tried playing around a little with casting and it hasn't worked so far. I'm not completely sure if this is because you can't accomplish this with casting or just because I haven't found the exact way to do it.

Another thing I have tried is subclassing TableListField and TableListField_Item. Inside the TableListField subclass I just change the itemClass and in TableListField_Item I overload the Fields method so I can check for every value if its an image object and then resize it. That's the theory at least (I haven't finished doing it). Anyone has a better idea?

Avatar
omarkohl

1 March 2011 at 2:53am Community Member, 30 Posts

I managed to do this. If someone is interested in a longer explanation just ask. If someone has a better solution please share!

You need to subclass TableListField and TableListField_Item. In TableListField you just set the $itemClass to your TableListField_Item class. In the TableListField_Item class you override the Fields() method and copy it exactly as in the original class.

Inside the foreach (of the Fields method) you add this:

if($fieldName === "Graphik"){
   $graphik = $this->item->Graphik();
   $value = $graphik->PaddedImage(100,70)->forTemplate();
}

"Graphik" is the name of the Image field.

And last but not least inside your Report class (that extends SS_Report) you override the getReportField() and copy it as in the original class. Then you change this line:

$tlf = new TableListField('ReportContent', $this->dataClass(), $columnTitles);

to use your extended TableListField class instead.

Avatar
frankmullenger

18 October 2011 at 10:55am Forum Moderator, 51 Posts

If you extend the Image class (GraphikImage) and have a class (Test) with this relation has_one : Graphik => GraphikImage, all you need to do is create a fortemplate() method on GraphikImage e.g:

function fortemplate() {
if ($Image = $this->Image()) return $Image->CroppedImage(40,40)->forTemplate();
else return '(No Image)';
}

So, GraphikImage would have a relation has_one: Image => Image in this case. A bit roundabout, but just an alternative.