ENH Use allowed view button for readonly GridField (#11228)

This commit is contained in:
Guy Sartorelli 2024-05-17 10:28:47 +12:00 committed by GitHub
parent cd77301de1
commit 56625081b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 54 additions and 3 deletions

View File

@ -276,9 +276,18 @@ class GridField extends FormField
} }
} }
// 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)) { 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); $copy->extend('afterPerformReadonlyTransformation', $this);

View File

@ -20,6 +20,8 @@ use SilverStripe\Forms\Tests\GridField\GridFieldTest\Cheerleader;
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team; use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldViewButton;
use SilverStripe\Forms\Tests\GridField\GridFieldReadonlyTest\GridFieldViewButtonReplacement;
use SilverStripe\Versioned\VersionedGridFieldState\VersionedGridFieldState; use SilverStripe\Versioned\VersionedGridFieldState\VersionedGridFieldState;
class GridFieldReadonlyTest extends SapphireTest class GridFieldReadonlyTest extends SapphireTest
@ -31,11 +33,28 @@ class GridFieldReadonlyTest extends SapphireTest
Cheerleader::class, 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. * 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. * 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. // Build a hasMany Relation via getComponents like ModelAdmin does.
$components = Team::get_one(Team::class) $components = Team::get_one(Team::class)
@ -67,6 +86,18 @@ class GridFieldReadonlyTest extends SapphireTest
$gridConfig $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() // 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 // function, if we rely on FormField to render into a ReadonlyField we'll get an error as HasManyRelation
// doesn't have a forTemplate() function. // doesn't have a forTemplate() function.

View File

@ -0,0 +1,11 @@
<?php
namespace SilverStripe\Forms\Tests\GridField\GridFieldReadonlyTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\GridField\GridFieldViewButton;
class GridFieldViewButtonReplacement extends GridFieldViewButton implements TestOnly
{
}