Skip to content

Commit

Permalink
NEW Add afterUpdateCMSFields method to DataObject. (#9819)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Oct 26, 2021
1 parent 49a7f08 commit 059d8aa
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/ORM/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2500,6 +2500,17 @@ protected function beforeUpdateCMSFields($callback)
$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.
Expand Down
5 changes: 5 additions & 0 deletions tests/php/ORM/DataExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,14 @@ public function testSubSubpageFieldGeneration()
// 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'));
Expand Down
4 changes: 4 additions & 0 deletions tests/php/ORM/DataExtensionTest/CMSFieldsBaseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ public function updateCMSFields(FieldList $fields)
if ($grandchildField = $fields->dataFieldByName('GrandchildFieldBeforeExtension')) {
$grandchildField->setTitle('GrandchildFieldBeforeExtension: Modified Title');
}

if ($grandchildPostField = $fields->dataFieldByName('GrandchildFieldAfterExtension')) {
$grandchildPostField->setTitle('GrandchildFieldAfterExtension: Modified Title');
}
}
}
4 changes: 3 additions & 1 deletion tests/php/ORM/DataExtensionTest/CMSFieldsChild.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -33,6 +34,7 @@ function (FieldList $fields) {
'updateCMSFields',
function (FieldList $fields) {
$fields->removeByName('ExtendedFieldRemove', true);
$fields->addFieldToTab('Root.Test', new TextField('ChildFieldAfterExtension'));
}
);

Expand Down
15 changes: 14 additions & 1 deletion tests/php/ORM/DataExtensionTest/CMSFieldsGrandChild.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
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
{
Expand All @@ -30,6 +30,19 @@ function (FieldList $fields) {
}
);

$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;
Expand Down

0 comments on commit 059d8aa

Please sign in to comment.