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.

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.

Customising the CMS /

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

DataObject and the Gridfield link to file


Go to End


5 Posts   2126 Views

Avatar
mi32dogs

Community Member, 75 Posts

12 March 2015 at 6:23pm

Hi, I’m new to silverstripe, and I’m trying to evaluate if I want to use silverstripe for my next projects.

I love it so far but I’m running into a problem with the DataObject and the Gridfield.

I created a dataobject that has a file via $has_one now what I’m trying to do is to have a link to this file in the Gridfield via the $summary_fields but I do not have any luck yet, does anybody knows of a good tutorial on how to do this? Also I like to add additional action buttons to it, a tutorial on that would also be greatly appreciated.

Avatar
martimiz

Forum Moderator, 1391 Posts

13 March 2015 at 10:09am

I don't have an answer for you right away, but have you seen the great new lessons department here: http://www.silverstripe.org/learn/lessons/

There's a brand new lesson on ModelAdmin (13) and it might give you some hints on casting and formatting gridfield datacells...

Avatar
Rhym

Community Member, 12 Posts

13 March 2015 at 10:47am

You can create a method to get the link to the file, and return it in your $summary_fields. I made you a paste as an example, I haven't tested it, but it should set you on the right direction: http://www.sspaste.com/paste/show/55020854ce4f5

Avatar
mi32dogs

Community Member, 75 Posts

13 March 2015 at 12:27pm

Edited: 13/03/2015 12:28pm

Thanks Rhym,

That worked great, the only thing is that it escaped the link html so I changed

 
if($this->File()->ID) {
      $html = '<a href="'.$this->File()->Link().'">'.$this->File()->Name.'</a>';
      return $html;
}

To

 
if($this->File()->ID) {
      $link= HTMLText::create();
      $link->setValue('<a href="'.$this-> File ()->Link().'" target="_blank"><div>'.$this-> File ()->Name.'</div></a>');
      return $link;
}

and now it displays the html correctly

Avatar
creativeSynergy

Community Member, 3 Posts

6 October 2015 at 11:25pm

Did you solve the "problem" that the gridfield item also gets opend?