From 009a32fbec243148d276e020ae1f7c9395b8040c Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Tue, 29 Jun 2021 11:21:41 +1200 Subject: [PATCH 1/2] ENH Allow GridField readonly components to be globally modified. --- src/Forms/GridField/GridField.php | 41 ++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/src/Forms/GridField/GridField.php b/src/Forms/GridField/GridField.php index f5b76cd07..c3b0a2635 100644 --- a/src/Forms/GridField/GridField.php +++ b/src/Forms/GridField/GridField.php @@ -52,6 +52,28 @@ class GridField extends FormField 'gridFieldAlterAction', ]; + /** + * Default globally configured readonly components. + * + * @see $readonlyComponents + * @var array + */ + private static $default_readonly_components = [ + GridField_ActionMenu::class, + GridFieldConfig_RecordViewer::class, + GridFieldButtonRow::class, + GridFieldDataColumns::class, + GridFieldDetailForm::class, + GridFieldLazyLoader::class, + GridFieldPageCount::class, + GridFieldPaginator::class, + GridFieldFilterHeader::class, + GridFieldSortableHeader::class, + GridFieldToolbarHeader::class, + GridFieldViewButton::class, + GridState_Component::class, + ]; + /** * Data source. * @@ -113,21 +135,7 @@ class GridField extends FormField * * @var array */ - protected $readonlyComponents = [ - GridField_ActionMenu::class, - GridFieldConfig_RecordViewer::class, - GridFieldButtonRow::class, - GridFieldDataColumns::class, - GridFieldDetailForm::class, - GridFieldLazyLoader::class, - GridFieldPageCount::class, - GridFieldPaginator::class, - GridFieldFilterHeader::class, - GridFieldSortableHeader::class, - GridFieldToolbarHeader::class, - GridFieldViewButton::class, - GridState_Component::class, - ]; + protected $readonlyComponents = []; /** * Pattern used for looking up @@ -144,6 +152,9 @@ class GridField extends FormField { parent::__construct($name, $title, null); + // Set readonly components for this gridfield. + $this->setReadonlyComponents($this->config()->get('default_readonly_components')); + $this->name = $name; if ($dataList) { From 365998f4453508cbb14d2e217a2003ae4a92ba7b Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Tue, 29 Jun 2021 11:23:58 +1200 Subject: [PATCH 2/2] ENH Add extension point for modifying readonly GridFields. Developers may want to amend a gridfield (either by modifying its components or amending the list) after it has been transformed to be readonly. One such modification may be replacing the GridFieldViewButton with something else - because components are instantiated with the "new" keyword this would be the only way to do that. --- src/Forms/GridField/GridField.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Forms/GridField/GridField.php b/src/Forms/GridField/GridField.php index c3b0a2635..a89104026 100644 --- a/src/Forms/GridField/GridField.php +++ b/src/Forms/GridField/GridField.php @@ -272,6 +272,8 @@ class GridField extends FormField $copyConfig->addComponent(new GridFieldViewButton); } + $copy->extend('afterPerformReadonlyTransformation', $this); + return $copy; }