From 02ae284e104887e72cc9dcac1ab1f175fef5e77d Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 3 Oct 2008 16:21:09 +0000 Subject: [PATCH] FEATURE Added DataObject->getFormFields() - uses DataObject->scaffoldFormFields() by default. Added DataObjectDecorator->updateFormFields() for easy customization ENHANCEMENT Calling DataObjectDecorator->updateCMSFields() in DataObject->getCMSFields() - was previously only called in SiteTree instances git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63623 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/DataObject.php | 27 ++++++++++++++++++++++++--- core/model/DataObjectDecorator.php | 23 +++++++++++++++++++---- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/core/model/DataObject.php b/core/model/DataObject.php index 216d6638d..a596e6113 100644 --- a/core/model/DataObject.php +++ b/core/model/DataObject.php @@ -1525,7 +1525,8 @@ class DataObject extends ViewableData implements DataObjectInterface { * Centerpiece of every data administration interface in Silverstripe, * which returns a {@link FieldSet} suitable for a {@link Form} object. * If not overloaded, we're using {@link scaffoldFormFields()} to automatically - * generate this set. + * generate this set. To customize, overload this method in a subclass + * or decorate onto it by using {@link DataObjectDecorator->updateCMSFields()}. * * * klass MyCustomClass extends DataObject { @@ -1533,7 +1534,7 @@ class DataObject extends ViewableData implements DataObjectInterface { * * public function getCMSFields() { * $fields = parent::getCMSFields(); - * $fields->push(new CheckboxField('CustomProperty')); + * $fields->addFieldToTab('Root.Content',new CheckboxField('CustomProperty')); * return $fields; * } * } @@ -1541,14 +1542,34 @@ class DataObject extends ViewableData implements DataObjectInterface { * * @see Good example of complex FormField building: SiteTree::getCMSFields() * - * @return FieldSet + * @return FieldSet Returns a TabSet for usage within the CMS - don't use for frontend forms. */ public function getCMSFields() { $fields = $this->scaffoldFormFields(); + // If we don't have an ID, then relation fields don't work if($this->ID) { $fields = $this->addScaffoldRelationFields($fields); } + + $this->extend('updateCMSFields', $fields); + + return $fields; + } + + + /** + * Used for simple frontend forms without relation editing + * or {@link TabSet} behaviour. Uses {@link scaffoldFormFields()} + * by default. To customize, either overload this method in your + * subclass, or decorate it by {@link DataObjectDecorator->updateFormFields()}. + * + * @return FieldSet Always returns a simple field collection without TabSet. + */ + public function getFormFields() { + $fields = $this->scaffoldFormFields(); + $this->extend('updateFormFields', $fields); + return $fields; } diff --git a/core/model/DataObjectDecorator.php b/core/model/DataObjectDecorator.php index a9e712890..29171e1eb 100755 --- a/core/model/DataObjectDecorator.php +++ b/core/model/DataObjectDecorator.php @@ -99,15 +99,30 @@ abstract class DataObjectDecorator extends Extension { /** * This function is used to provide modifications to the form in the CMS - * by the decorator. - * By default, no changes are made - if you want you can overload this - * function. + * by the decorator. By default, no changes are made. + * Please consider using {@link updateFormFields()} to globally add + * formfields to the record. The method {@link updateCMSFields()} + * should just be used to add or modify tabs, or fields which + * are specific to the CMS-context. * - * @param FieldSet $fields The FieldSet to modify. + * Caution: Use {@link FieldSet->addFieldToTab()} to add fields. + * + * @param FieldSet $fields FieldSet with a contained TabSet */ function updateCMSFields(FieldSet &$fields) { } + /** + * This function is used to provide modifications to the form in the CMS + * by the decorator. + * + * Caution: Use {@link FieldSet->push()} to add fields. + * + * @param FieldSet $fields FieldSet without TabSet nesting + */ + function updateFormFields(FieldSet &$fields) { + } + /** * this function is used to provide modifications to the summary fields in CMS * by the decorator