mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
NEW Add afterUpdateCMSFields method to DataObject. (#9819)
This commit is contained in:
parent
49a7f08c07
commit
059d8aac0a
@ -2500,6 +2500,17 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
||||
$this->beforeExtending('updateCMSFields', $callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows user code to hook into DataObject::getCMSFields after updateCMSFields
|
||||
* being called on extensions
|
||||
*
|
||||
* @param callable $callback The callback to execute
|
||||
*/
|
||||
protected function afterUpdateCMSFields(callable $callback)
|
||||
{
|
||||
$this->afterExtending('updateCMSFields', $callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Centerpiece of every data administration interface in Silverstripe,
|
||||
* which returns a {@link FieldList} suitable for a {@link Form} object.
|
||||
|
@ -259,9 +259,14 @@ class DataExtensionTest extends SapphireTest
|
||||
// Check child fields removed by grandchild in beforeUpdateCMSFields
|
||||
$this->assertEmpty($fields->dataFieldByName('ChildFieldBeforeExtension')); // Removed by grandchild class
|
||||
|
||||
// Check child fields removed by grandchild in afterUpdateCMSFields
|
||||
$this->assertEmpty($fields->dataFieldByName('ChildFieldAfterExtension')); // Removed by grandchild class
|
||||
|
||||
// Check grandchild field modified by extension
|
||||
$this->assertNotEmpty($preExtendedField = $fields->dataFieldByName('GrandchildFieldBeforeExtension'));
|
||||
$this->assertNotEmpty($postExtendedField = $fields->dataFieldByName('GrandchildFieldAfterExtension'));
|
||||
$this->assertEquals($preExtendedField->Title(), 'GrandchildFieldBeforeExtension: Modified Title');
|
||||
$this->assertEquals($postExtendedField->Title(), 'GrandchildFieldAfterExtension');
|
||||
|
||||
// Post-extension fields
|
||||
$this->assertNotEmpty($fields->dataFieldByName('ChildField'));
|
||||
|
@ -29,5 +29,9 @@ class CMSFieldsBaseExtension extends DataExtension implements TestOnly
|
||||
if ($grandchildField = $fields->dataFieldByName('GrandchildFieldBeforeExtension')) {
|
||||
$grandchildField->setTitle('GrandchildFieldBeforeExtension: Modified Title');
|
||||
}
|
||||
|
||||
if ($grandchildPostField = $fields->dataFieldByName('GrandchildFieldAfterExtension')) {
|
||||
$grandchildPostField->setTitle('GrandchildFieldAfterExtension: Modified Title');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,8 @@ class CMSFieldsChild extends CMSFieldsBase implements TestOnly
|
||||
|
||||
private static $db = [
|
||||
'ChildField' => 'Varchar(255)',
|
||||
'ChildFieldBeforeExtension' => 'Varchar(255)'
|
||||
'ChildFieldBeforeExtension' => 'Varchar(255)',
|
||||
'ChildFieldAfterExtension' => 'Varchar(255)'
|
||||
];
|
||||
|
||||
public function getCMSFields()
|
||||
@ -33,6 +34,7 @@ class CMSFieldsChild extends CMSFieldsBase implements TestOnly
|
||||
'updateCMSFields',
|
||||
function (FieldList $fields) {
|
||||
$fields->removeByName('ExtendedFieldRemove', true);
|
||||
$fields->addFieldToTab('Root.Test', new TextField('ChildFieldAfterExtension'));
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -8,7 +8,7 @@ use SilverStripe\Forms\TextField;
|
||||
use SilverStripe\ORM\Tests\DataExtensionTest\CMSFieldsChild;
|
||||
|
||||
/**
|
||||
* Third level test class, testing that beforeExtendingCMSFields can be nested
|
||||
* Third level test class, testing that beforeExtendingCMSFields and afterExtendingCMSFields can be nested
|
||||
*/
|
||||
class CMSFieldsGrandChild extends CMSFieldsChild implements TestOnly
|
||||
{
|
||||
@ -30,6 +30,19 @@ class CMSFieldsGrandChild extends CMSFieldsChild implements TestOnly
|
||||
}
|
||||
);
|
||||
|
||||
$this->afterUpdateCMSFields(
|
||||
function (FieldList $fields) {
|
||||
// Remove field from parent's afterExtendingCMSFields
|
||||
$fields->removeByName('ChildFieldAfterExtension', true);
|
||||
|
||||
// Adds own post-extension field
|
||||
$fields->addFieldToTab(
|
||||
'Root.Test',
|
||||
new TextField('GrandchildFieldAfterExtension', 'GrandchildFieldAfterExtension')
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
$fields = parent::getCMSFields();
|
||||
$fields->addFieldToTab('Root.Test', new TextField('GrandchildField'));
|
||||
return $fields;
|
||||
|
Loading…
Reference in New Issue
Block a user