From 56625081b43d2d49530fae577cdaa3d8fa572c17 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Fri, 17 May 2024 10:28:47 +1200 Subject: [PATCH] ENH Use allowed view button for readonly GridField (#11228) --- src/Forms/GridField/GridField.php | 13 ++++++-- .../Forms/GridField/GridFieldReadonlyTest.php | 33 ++++++++++++++++++- .../GridFieldViewButtonReplacement.php | 11 +++++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 tests/php/Forms/GridField/GridFieldReadonlyTest/GridFieldViewButtonReplacement.php diff --git a/src/Forms/GridField/GridField.php b/src/Forms/GridField/GridField.php index d42fb7b34e9..1707fafdba3 100644 --- a/src/Forms/GridField/GridField.php +++ b/src/Forms/GridField/GridField.php @@ -276,9 +276,18 @@ public function performReadonlyTransformation() } } - // If the edit button has been removed, replace it with a view button + // If the edit button has been removed, replace it with a view button if one is allowed if ($hadEditButton && !$copyConfig->getComponentByType(GridFieldViewButton::class)) { - $copyConfig->addComponent(GridFieldViewButton::create()); + $viewButtonClass = null; + foreach ($allowedComponents as $componentClass) { + if (is_a($componentClass, GridFieldViewButton::class, true)) { + $viewButtonClass = $componentClass; + break; + } + } + if ($viewButtonClass) { + $copyConfig->addComponent($viewButtonClass::create()); + } } $copy->extend('afterPerformReadonlyTransformation', $this); diff --git a/tests/php/Forms/GridField/GridFieldReadonlyTest.php b/tests/php/Forms/GridField/GridFieldReadonlyTest.php index 9a0742d89f7..27b51096f4c 100644 --- a/tests/php/Forms/GridField/GridFieldReadonlyTest.php +++ b/tests/php/Forms/GridField/GridFieldReadonlyTest.php @@ -20,6 +20,8 @@ use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team; use SilverStripe\Dev\SapphireTest; use SilverStripe\Forms\GridField\GridField; +use SilverStripe\Forms\GridField\GridFieldViewButton; +use SilverStripe\Forms\Tests\GridField\GridFieldReadonlyTest\GridFieldViewButtonReplacement; use SilverStripe\Versioned\VersionedGridFieldState\VersionedGridFieldState; class GridFieldReadonlyTest extends SapphireTest @@ -31,11 +33,28 @@ class GridFieldReadonlyTest extends SapphireTest Cheerleader::class, ]; + public function provideReadOnlyTransformation(): array + { + return [ + [ + 'viewButtonClass' => null, + ], + [ + 'viewButtonClass' => GridFieldViewButton::class, + ], + [ + 'viewButtonClass' => GridFieldViewButtonReplacement::class, + ], + ]; + } + /** * The CMS can set the value of a GridField to be a hasMany relation, which needs a readonly state. * This test ensures GridField has a readonly transformation. + * + * @dataProvider provideReadOnlyTransformation */ - public function testReadOnlyTransformation() + public function testReadOnlyTransformation(?string $viewButtonClass) { // Build a hasMany Relation via getComponents like ModelAdmin does. $components = Team::get_one(Team::class) @@ -67,6 +86,18 @@ public function testReadOnlyTransformation() $gridConfig ); + if ($viewButtonClass !== GridFieldViewButton::class) { + $allowedComponents = $gridField->getReadonlyComponents(); + $viewButtonIndex = array_search(GridFieldViewButton::class, $allowedComponents); + if ($viewButtonIndex !== false) { + unset($allowedComponents[$viewButtonIndex]); + } + if ($viewButtonClass !== null) { + $allowedComponents[] = $viewButtonClass; + } + $gridField->setReadonlyComponents($allowedComponents); + } + // Model Admin sets the value of the GridField directly to the relation, which doesn't have a forTemplate() // function, if we rely on FormField to render into a ReadonlyField we'll get an error as HasManyRelation // doesn't have a forTemplate() function. diff --git a/tests/php/Forms/GridField/GridFieldReadonlyTest/GridFieldViewButtonReplacement.php b/tests/php/Forms/GridField/GridFieldReadonlyTest/GridFieldViewButtonReplacement.php new file mode 100644 index 00000000000..9f26df5aafa --- /dev/null +++ b/tests/php/Forms/GridField/GridFieldReadonlyTest/GridFieldViewButtonReplacement.php @@ -0,0 +1,11 @@ +