Jump to:

23459 Posts in 18906 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » GridField - Pre-populate the relationship on the add form

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

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

Page: 1
Go to End
Author Topic: 466 Views
  • MrNick
    Avatar
    Community Member
    9 Posts

    GridField - Pre-populate the relationship on the add form Link to this post

    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

  • Nobrainer Web
    Avatar
    Community Member
    134 Posts

    Re: GridField - Pre-populate the relationship on the add form Link to this post

    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

  • MrNick
    Avatar
    Community Member
    9 Posts

    Re: GridField - Pre-populate the relationship on the add form Link to this post

    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?

  • Nobrainer Web
    Avatar
    Community Member
    134 Posts

    Re: GridField - Pre-populate the relationship on the add form Link to this post

    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.

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