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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

GridField - Pre-populate the relationship on the add form


Go to End


4 Posts   1258 Views

Avatar
MrNick

Community Member, 9 Posts

8 May 2013 at 12:12am

Good morning,

I have a page with a $has_many relationship to VideoSnippets. VideoSnippets class extends from DataObject and includes a $has_one relationship back to my page. It all works fine, but in the CMS when I go to my page, and click the big green button to add a VideoSnippet to the page I am on, the droplist for the page (relationship) is blank. I would expect this to be populated since it knows (shows in the bread crumbs) the page I clicked the Add on.

The droplist for the relationship is blank in the form, and hitting save will select the correct page. However is there a way I can have it already selected, or, even better, don't even show the relationship droplist?

Thanks

Avatar
Nobrainer Web

Community Member, 138 Posts

8 May 2013 at 5:38am

Hi,

You can easily remove the field by adding $fields->removeByName('PageID'); into the getCMSFields function of your DataObject.

I have been wondering why this field get's shown, but i guess it has to do with the fields be generated by the form scaffolidng.
The really weird part is that it will not allow you to actually change the relation to another page, if you try, it just sets the same page again.

Hope it helps

Avatar
MrNick

Community Member, 9 Posts

8 May 2013 at 8:12am

Perfect, that worked great thanks!.
I didn't think to do that, as I didn't think that method would exist/be called on the DataObject, I thought it came off page.

It would be even beter, if once it saved it redirected back to the gridview, instead of just reloading the page with green "Saved" message.

That wouldn't be a method on the DataObject, that would be something in the core to fix yea?

Avatar
Nobrainer Web

Community Member, 138 Posts

8 May 2013 at 6:19pm

There is an extension from UncleCheese that does exactly that (and a bit more)
https://github.com/unclecheese/silverstripe-gridfield-betterbuttons

I have only tried it on 3.1 but works there, however it returns you to the main tab of the page.