Jump to:

5542 Posts in 1739 Topics by 1225 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Getting first image thumbnail from a has_many

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

Page: 1
Go to End
Author Topic: 331 Views
  • Matty Balaam
    Avatar
    Community Member
    69 Posts

    Getting first image thumbnail from a has_many Link to this post

    I have a gridfield in SectionPage managing a has_many dataobject 'Sections' => 'Section', which in turn has_many 'Gallery' => 'GalleryItem' which has_one image.

    How can I get the Gridfield to return a thumbnail of the first GalleryItem?

    I have tried many variations in Section.php, but feel this is the closest I have got:

       public function getThumbnail() {
          return $this->Gallery()->First()->Photo()->CMSThumbnail();
       }

    This gives me a 'Call to a member function Photo() on a non-object in...' error.

    Can anyone give me some help with this problem?

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: Getting first image thumbnail from a has_many Link to this post

    Make sure $this->Gallery()->First() is returning an object and not just a VIewableData array. I know I've run into this in the past

    It may alos work to create a getThumbnial on GalleryItem and set it as virtual property

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