Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » SilverStripe 3: GridField with many_many_extraFields

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

Page: 1
Go to End
Author Topic: 1780 Views
  • avengex
    Avatar
    Community Member
    11 Posts

    SilverStripe 3: GridField with many_many_extraFields Link to this post

    Hey guys,

    I've had a look around the codebase to figure out how to get extraFields working but I have to say I'm stumped.

    My current getCMSFields GridField setup is:

    $gfConfig = GridFieldConfig_RelationEditor::create();
    $amenityList = $this->Amenities();
    $amField = new GridField('Amenities', null, $amenityList, $gfConfig);

    The Amenity getCMSFields is:

    function getCMSFields() {
       return new FieldList(
          new TextField('Title', 'Name of amenity', null, 40),
          new TextField('TravelTime', 'Travel time to get there', null, 40)
       );
    }

    For TravelTime, I've tried various constructs like ParentPage.TravelTime etc, but no luck.

    Has anyone done this before?

  • BenWu
    Avatar
    Community Member
    87 Posts

    Re: SilverStripe 3: GridField with many_many_extraFields Link to this post

    I am looking for the same thing. According to this http://www.silverstripe.org/general-questions/show/7395 it looks like there is no built-in function for you to edit the many_many_extraFields data.

    I think there are 2 ways to get around this:
    1. Break the many_many relation down to 2 has_many relations:
    check the example here http://www.balbuss.com/multiple-many-many-s-of-the-same-class/

    2. Write your own formfield
    In my project, as it is too late to use Method 1, I have to write my own formfield so that it can be added back into the admin. I am using the SubmittedFormField in the UserForm module as an example to write my scripts.

    It's a shame it is hard for us to do this when SS3 should have taken care of this.
    I have seen the Datagrid proposal here http://open.silverstripe.org/wiki/development/DataGrid which is exactly what I want when I have large amount of data to manage. However, it looks like this proposal is ignored.

    If you have any quick way to do this, please share.

  • martimiz
    Avatar
    Forum Moderator
    1035 Posts

    Re: SilverStripe 3: GridField with many_many_extraFields Link to this post

    I too stumbled upon this issue again, and I really feel that this option should either be implemented in a simple to use way (preferred ) or be removed entirely. I was thinking of posting this at the core dev group, to see what others think. Looks Like someone really should now...

    Martine

  • avengex
    Avatar
    Community Member
    11 Posts

    Re: SilverStripe 3: GridField with many_many_extraFields Link to this post

    Hey guys,

    I've found the way to do this. It's undocumented so far, as far as I can see anyway. Turns out it wasn't impossible after all - the API has changed though.

    I did some digging in framework and found this inside GridFieldDataForm.php line 450:

    $extraData = (isset($data['ManyMany'])) ? $data['ManyMany'] : null;

    The highlighted line basically looks for ManyMany as an associative-array style entry in the CMS fields.

    So the way around it is luckily quite simple!

    Inside your many_many object's getCMSFields that you wish to add extra data to, you can add the field like so:

    new TextField('ManyMany[ExtraFieldName]', 'Extra field')

    Where "ExtraFieldName" is the name of the extraField you defined inside the $many_many_extraFields static.

  • apiening
    Avatar
    Community Member
    55 Posts

    Re: SilverStripe 3: GridField with many_many_extraFields Link to this post

    Hey guys,

    I came accross the same issue yesterday. My problem is that I have a complex model where displaying the extra fields in getCMSFields is tricky. The context in which getCMSFields gets called varies, some extra fields may not be valid in some contexts. So I thought that the best place to display the extra form fields for my application is right in the gridfields list view. I made a very lightweight module and published it on github. In case anybody likes the idea of inline editing the extra fields in the gridfield, here it is:

    https://github.com/smindel/silverstripe-gridfieldeditablemanymanyextracolumns

    (hope you like the name)

    Cheers

    Andy

  • lx
    Avatar
    Community Member
    81 Posts

    Re: SilverStripe 3: GridField with many_many_extraFields Link to this post

    avengex, do you know if its possible to change the form that shows up when clicking on the edit button in the grid, to only show all formfields for the many_many_extrafields.

    I have about 7-8 many_many_extrafields. And clicking on edit should only allow me to edit these.

    I dont want to make changes to all properties of the User class.
    To manage users i already have a modeladmin.

    The problem is, that my modeladmin uses getCMSFields() to generate the Form. And the edit button in the grid also uses getCMSFields(). Is there a way to detect if i am in the modeladmin section or if i clicked on edit in the many_many grid.

    thanks
    lx

  • avengex
    Avatar
    Community Member
    11 Posts

    Re: SilverStripe 3: GridField with many_many_extraFields Link to this post

    Yes, this is slightly off topic, but it's possible. In 2.4 you could pass in the $detailFormFields of the DataObjectManager, and in 3.1 you now call setFields on a GridFieldDetailForm, passing in a FieldList object.

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