Jump to:

5537 Posts in 1737 Topics by 1223 members

Customising the CMS

SilverStripe Forums » Customising the CMS » SS 2.4 -> 3.1 site upgrade. Auto edit forms for Gridfield [Solved]

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

Page: 1
Go to End
Author Topic: 310 Views
  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    SS 2.4 -> 3.1 site upgrade. Auto edit forms for Gridfield [Solved] Link to this post

    So I'm migrating an existing 2.4 site to 3.1. Almost done. Just tidying up the CMS admin now.

    One of the biggest challenges with this migration has been understanding the new GridField component. Or to be more precise, exactly how one can override the automatic scaffolding done when you edit/add a record. That is, how to customize the resulting form. Really, there is little in the docs, and many of the examples I have come across differ slightly.

    Example. I have a Tour (extends Page). Tour has_many TourDate (extends DataObject). TourDate in turn has_one Tour. In 2.4 I had a ComplexTableField in getCMSFields on Tour. On getCMSFields_forPopup on TourDate, I am simple creating a FieldSet, pushing fields from TourDate onto it, and returning it.

    With 3.1 and GridField however, it seems I have to use getCMSFields on TourDate. Get the parent getCMSFields, and then add to it using addFieldToTab. Which is fine, but for one thing.

    The automatic scaffolding is adding a dropdown for the has_one Tour relationship at the top of the TourDate form. I have tried $fields->removeByName('Tour') and $fields->removeFieldFromTab('Root.Main', 'Tour') to no avail.

    My question is am I doing this correctly? I've seen examples of people still using getCMSFields_forPopup in the dataobject. I've seen some method called getEditForm (but that might be cross over with ModelAdmin work I am doing). The relationship here should be added automatically as it was with ComplexTableField. I don't want a user to be able to change it on add/edit.

    Pastes if it's easier:
    http://www.sspaste.com/paste/show/531b9d9f5f3fb

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: SS 2.4 -> 3.1 site upgrade. Auto edit forms for Gridfield [Solved] Link to this post

    The name of the field you want to remove is TourID. getEditForm's the method on ModelAdmin, and getCMSFields_forPopup won't work at all with GridField.

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: SS 2.4 -> 3.1 site upgrade. Auto edit forms for Gridfield [Solved] Link to this post

    Thanks Simon,

    I just this moment worked it out by studying someone else's module code. Without having to manually remove anything.

    In the TourDate getCMSFields(), I changed the code from this:

    function getCMSFields() {

          $fields = parent::getCMSFields();
    $fields->removeFieldFromTab('Root.Main', 'Tour');
    $fields->removeFieldFromTab('Root.Main','ShortTour');

          $start_date = new DateField( 'StartDate', 'Start Date' );
          $end_date = new DateField( 'EndDate', 'End Date' );
          $start_date->setConfig('showcalendar', true);
          $end_date->setConfig('showcalendar', true);

    $fields->addFieldToTab('Root.Main', $start_date);
    $fields->addFieldToTab('Root.Main', $end_date);
          $fields->addFieldToTab('Root.Main', new NumericField( 'AvailableSeats', 'Available Seats Left' ));
    $fields->addFieldToTab('Root.Main', new LiteralField('SpecialsHeader', '<h2>Special</h2>'));
    $fields->addFieldToTab('Root.Main', new CurrencyField('TourSpecialCostUS', 'Special Price - US Dollar'));
    $fields->addFieldToTab('Root.Main', new CurrencyField('TourSpecialCostUK', 'Special Price - UK Pound'));
    $fields->addFieldToTab('Root.Main', new CurrencyField('TourSpecialCostEU', 'Special Price - EU Euro'));
    $fields->addFieldToTab('Root.Main', new CurrencyField('TourSpecialCostAU', 'Special Price - AUS Dollar'));
    $fields->addFieldToTab('Root.Main', new CurrencyField('TourSpecialCostNZ', 'Special Price - NZ Dollar'));
    $fields->addFieldToTab('Root.Main', new TextAreaField('TourSpecialText', 'Description text for special'));
          
          $embargoStart = new DateField('TourSpecialEmbargoStart', 'Special Start Date');
          $embargoStart->setConfig('showcalendar', true);
          $embargoEnd = new DateField('TourSpecialEmbargoEnd', 'Special End Date');
          $embargoEnd->setConfig('showcalendar', true);
    $fields->addFieldToTab('Root.Main', $embargoStart);
    $fields->addFieldToTab('Root.Main', $embargoEnd);      

          return $fields;

       }

    To this:

    function getCMSFields() {

          $startDate = new DateField( 'StartDate', 'Start Date' );
          $endDate = new DateField( 'EndDate', 'End Date' );
          $startDate->setConfig('showcalendar', true);
          $endDate->setConfig('showcalendar', true);

          $embargoStart = new DateField('TourSpecialEmbargoStart', 'Special Start Date');
          $embargoStart->setConfig('showcalendar', true);
          $embargoEnd = new DateField('TourSpecialEmbargoEnd', 'Special End Date');
          $embargoEnd->setConfig('showcalendar', true);

    return new FieldList(
    $startDate,
    $endDate,
    new NumericField( 'AvailableSeats', 'Available Seats Left' ),
    new CurrencyField('TourSpecialCostUS', 'Special Price - US Dollar'),
    new CurrencyField('TourSpecialCostUK', 'Special Price - UK Pound'),
    new CurrencyField('TourSpecialCostEU', 'Special Price - EU Euro'),
    new CurrencyField('TourSpecialCostAU', 'Special Price - AUS Dollar'),
    new CurrencyField('TourSpecialCostNZ', 'Special Price - NZ Dollar'),
    new TextAreaField('TourSpecialText', 'Description text for special'),
    $embargoStart,
    $embargoEnd
    );

       }

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: SS 2.4 -> 3.1 site upgrade. Auto edit forms for Gridfield [Solved] Link to this post

    Code formatting is pretty bad, so in a nutshell:

    * No $fields = parent::getCMSFields
    * No $fields->addFieldToTab for each field
    * Simply return a new FieldList as you would like the form

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