diff --git a/src/Forms/GridField/GridField.php b/src/Forms/GridField/GridField.php index 0585e3703..a180fa5dc 100644 --- a/src/Forms/GridField/GridField.php +++ b/src/Forms/GridField/GridField.php @@ -113,11 +113,13 @@ class GridField extends FormField 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, @@ -241,16 +243,22 @@ class GridField extends FormField { $copy = clone $this; $copy->setReadonly(true); + $copyConfig = $copy->getConfig(); // get the whitelist for allowable readonly components $allowedComponents = $this->getReadonlyComponents(); foreach ($this->getConfig()->getComponents() as $component) { // if a component doesn't exist, remove it from the readonly version. if (!in_array(get_class($component), $allowedComponents)) { - $copy->getConfig()->removeComponent($component); + $copyConfig->removeComponent($component); } } + // As the edit button may have been removed, add a view button if it doesn't have one + if (!$copyConfig->getComponentByType(GridFieldViewButton::class)) { + $copyConfig->addComponent(new GridFieldViewButton); + } + return $copy; } @@ -290,6 +298,18 @@ class GridField extends FormField return $this; } + /** + * @param bool $readonly + * + * @return $this + */ + public function setReadonly($readonly) + { + parent::setReadonly($readonly); + $this->getState()->Readonly = $readonly; + return $this; + } + /** * @return ArrayList */ @@ -1009,6 +1029,9 @@ class GridField extends FormField } if ($request->getHeader('X-Pjax') === 'CurrentField') { + if ($this->getState()->Readonly) { + $this->performDisabledTransformation(); + } return $this->FieldHolder(); }