Jump to:

1999 Posts in 1421 Topics by 613 members

Form Questions

SilverStripe Forums » Form Questions » Images in TableListField

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

Page: 1
Go to End
Author Topic: 1010 Views
  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Images in TableListField Link to this post

    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.

  • omarkohl
    Avatar
    Community Member
    30 Posts

    Re: Images in TableListField Link to this post

    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?

  • omarkohl
    Avatar
    Community Member
    30 Posts

    Re: Images in TableListField Link to this post

    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.

  • frankmullenger
    Avatar
    Forum Moderator
    51 Posts

    Re: Images in TableListField Link to this post

    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.

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