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.
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
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?
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.
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.