Field Scaffolding
Coming to SilverStripe 2.3 is support for field scaffolding. That is, SilverStripe DataObjects will be able to create a default FieldSet that can be used for editing them.
STATUS: Field scaffolding is part of branches/roa, a development branch that will be merged into trunk "soon".
Implementation
Every DBField sub-class can define a scaffoldFormField() method, that will return a FormField object suitable for adding to a field set.
<?php class Boolean extends DBField { // ... public function scaffoldFormField($title = null) { return new CheckboxField($this->name, $title); } } ?>
The DataObject class has 3 methods that make use of this:
- scaffoldFormFields($fieldClasses = null): Generate a FieldSet from the $db and $has_one fields.
- addScaffoldRelationFields(&$fields): Update the given FieldSet, adding ComplexTableFields for $has_many and $many_many relations, 1 per tab. The original fields will be placed on the "Main" tab.
- getCMSFields(): Call the two methods above to produce a tabbed fieldset, similar to SiteTree::getCMSFields().
Currently, the implementation of relation scaffolding is hard-coded into the DataObject class. For many-many relations, we have developed a modified version of ComplexTableField that does ajax-autocomplete in the detail form to determine whether we should create a new record or link to an existing one. We will probably replace ManyManyComplexTableField with this customised control; the current MMCTF isn't very usable on larger data-sets.
Usage
Application developers can now call getCMSFields() on any object and place the resulting field set in a form. The result is that applications can be designed without needing to know what sort of data is being acted on. ModelAdmin is the first major roll-out of this.
Future work
Currently the generation of tabs for relations is kind of application-specific, and it's likely that people will want alternative ways of scaffolding the fields. We should probably refactor the methods on DataObject to make this easier.
