mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENH Use allowed view button for readonly GridField (#11228)
This commit is contained in:
parent
cd77301de1
commit
56625081b4
@ -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);
|
||||||
|
@ -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.
|
||||||
|
@ -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
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user