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.

Customising the CMS /

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


Reply


4 Posts   362 Views

Avatar
Double-A-Ron

Community Member, 604 Posts

9 March 2014 at 12:01pm

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

Avatar
simon_w

Forum Moderator, 474 Posts

9 March 2014 at 1:05pm

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.

Avatar
Double-A-Ron

Community Member, 604 Posts

9 March 2014 at 1:11pm

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
);

   }

Avatar
Double-A-Ron

Community Member, 604 Posts

9 March 2014 at 1:13pm

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