NEW Add afterUpdateCMSFields method to DataObject. (#9819)

This commit is contained in:
GuySartorelli 2021-10-26 16:05:11 +13:00 committed by GitHub
parent 49a7f08c07
commit 059d8aac0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 2 deletions

View File

@ -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.

View File

@ -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'));

View File

@ -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');
}
}
}

View File

@ -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'));
}
);

View File

@ -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;