silverstripe/sapphire — pull request #1860 by tractorcow (2013-05-09 01:42:22+00:00)
Title: NEW Added beforeExtending, afterExtending, and beforeUpdateCMSFields

Added beforeExtending, afterExtending, and beforeUpdateCMSFields to provide a finer degree of control over how classes interact with their own extensions.

Please see https://github.com/silverstripe/silverstripe-cms/pull/692 for the first implementation of this method, and subsequent discussion, followed by https://github.com/silverstripe/silverstripe-cms/pull/694 for the second implementation. This third implementation supersedes these.

Modules such as Blog use disableCMSFieldsExtensions / enableCMSFieldsExtensions around field manipulation code in order to delay extension of CMS fields. See https://github.com/silverstripe/silverstripe-blog/blob/master/code/BlogEntry.php#L62

This code simply formalises it, adds test cases, and documentation. Hopefully this will make user code that performs such manipulation a bit more explicit and readable.

E.g.

```php

public function getCMSFields() {

// Before updateCMSFields we want to add some text fields, perhaps
// to be later interacted with with modules such as Translatable
$this->beforeUpdateCMSFields(function(FieldList $fields) {
$fields->addFieldToTab('Root.Main', new TextField('Details'));
});

// after extending updateCMSFields we may want to interact with fields added by extensions
$fields = parent::getCMSFields();
$fields->removeByName('GoogleAnalytics');
return $fields;
}
```

Resolves ticket at https://github.com/silverstripe/sapphire/issues/1362