Comments (6)
The problem with swapping this order is that you then have the opposite problem; user code that extends getCMSFields is then no longer able to update fields added by extensions.
The real question is, does the convention need to change?
I would argue not, as the above solution can be solved by user code that determines when fields should be overridden or not.
See the blog module for an example of how this is done:
function getCMSFields() {
SiteTree::disableCMSFieldsExtensions();
$fields = parent::getCMSFields();
SiteTree::enableCMSFieldsExtensions();
// do stuff
$this->extend('updateCMSFields', $fields);
return $fields;
}
I think a better question is, can we make it simpler and easier for users to do the above? (rather than changing the convention).
Is this better?
SiteTree.php
/**
* Delays extension of getCMSFields
* @param callable $callback
* @return FieldList
*/
protected function beforeExtendingCMSFields($callback) {
if(!self::$runCMSFieldsExtensions) {
user_error('Calls to SiteTree::beforeExtendingCMSFields cannot be nested', E_USER_ERROR);
}
self::disableCMSFieldsExtensions();
$fields = call_user_func(array($this, "parent::getCMSFields"));
$callback($fields);
self::enableCMSFieldsExtensions();
$this->extend('updateCMSFields', $fields);
return $fields;
}
MyPage.php
public function getCMSFields() {
$fields = $this->beforeExtendingCMSFields(function(FieldList &$fields){
$fields->addFieldToTab('Root.Main', new TextField('Test', 'Test'), 'Content');
});
// after extending updateCMSFields
$fields->removeByName('Root.GoogleSitemaps');
return $fields;
}
from silverstripe-framework.
Like your thinking! That defiantly makes the code a bit more explicit as to what it's trying to achieve rather than the disable() calls.
from silverstripe-framework.
Shame the code above doesn't work very well, but I'll rough something up and do some better testing.
The problem is that there's no object context in a closure, so calls to parent::getCMSFields is impossible, and workarounds are flakey.
from silverstripe-framework.
Is there a way to flag issues on Github, or can someone assign this issue to me?
from silverstripe-framework.
Issues can only be assigned to people in the organisation.
from silverstripe-framework.
Pull request is in the CMS module, including test cases. I've resolved the flakiness issue by adding a $parentClass specifier, allowing user code to tweak the behaviour where otherwise errors or infinite loops would occur.
from silverstripe-framework.
Related Issues (20)
- Session based GridField state management HOT 8
- SPIKE: Investigate ways to get configurable cache adapter prior to loading config HOT 3
- Make extension hook methods protected - part 2
- Migrate from m1/env to symfony/dotenv
- Refactor shortcode parsing HOT 2
- Move `ViewableData::Me()` out of `ViewableData` and into the template layer
- Better casting in `ViewableData`
- Default to session gridifield state manager in CMS 6
- text collector doesn't collect when parameters are passed as a variable HOT 3
- SearchableDropdownTrait::getSource doesn't respect lazy-loading HOT 5
- SQLSelect regression with subqueries HOT 16
- Deprecated getIterator function called in SSViewer_Scope HOT 2
- Permission check for 'Not strict check' is wrong AFAIK HOT 5
- Remember Me functionality fails when renewal is interrupted HOT 10
- SearchableDropdownTrait::saveInto calls DataObject::write HOT 4
- Sake script failure when PHP binary path contains spaces in SilverStripe 5.2.2 HOT 1
- Drop support for MySQL 5 in CMS 6
- `performReadonlyTransformation()` for `SearchableDropdownField` and `SearchableMultiDropdownField` doesn't respect lazy loading HOT 4
- DBForeignKey scaffolding uses wrong parameters to create SearchableDropdownField HOT 1
- HTMLValue setContent preg_replace stripping <header> tags HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from silverstripe-framework.