Skip to content

Commit

Permalink
ENH Use allowed view button for readonly GridField (#11228)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored May 16, 2024
1 parent cd77301 commit 5662508
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/Forms/GridField/GridField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 32 additions & 1 deletion tests/php/Forms/GridField/GridFieldReadonlyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{

}

0 comments on commit 5662508

Please sign in to comment.