Persist GridField readonly state, add view button (#8535)

* Persist GridField readonly state, add view button

* Minor clarity fixes
This commit is contained in:
Luke Edwards 2018-11-01 11:12:52 +13:00 committed by Aaron Carlino
parent 511c368e8c
commit c7b8b80e8b

View File

@ -113,11 +113,13 @@ class GridField extends FormField
protected $readonlyComponents = [ protected $readonlyComponents = [
GridField_ActionMenu::class, GridField_ActionMenu::class,
GridFieldConfig_RecordViewer::class, GridFieldConfig_RecordViewer::class,
GridFieldButtonRow::class,
GridFieldDataColumns::class, GridFieldDataColumns::class,
GridFieldDetailForm::class, GridFieldDetailForm::class,
GridFieldLazyLoader::class, GridFieldLazyLoader::class,
GridFieldPageCount::class, GridFieldPageCount::class,
GridFieldPaginator::class, GridFieldPaginator::class,
GridFieldFilterHeader::class,
GridFieldSortableHeader::class, GridFieldSortableHeader::class,
GridFieldToolbarHeader::class, GridFieldToolbarHeader::class,
GridFieldViewButton::class, GridFieldViewButton::class,
@ -241,16 +243,22 @@ class GridField extends FormField
{ {
$copy = clone $this; $copy = clone $this;
$copy->setReadonly(true); $copy->setReadonly(true);
$copyConfig = $copy->getConfig();
// get the whitelist for allowable readonly components // get the whitelist for allowable readonly components
$allowedComponents = $this->getReadonlyComponents(); $allowedComponents = $this->getReadonlyComponents();
foreach ($this->getConfig()->getComponents() as $component) { foreach ($this->getConfig()->getComponents() as $component) {
// if a component doesn't exist, remove it from the readonly version. // if a component doesn't exist, remove it from the readonly version.
if (!in_array(get_class($component), $allowedComponents)) { 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; return $copy;
} }
@ -290,6 +298,18 @@ class GridField extends FormField
return $this; return $this;
} }
/**
* @param bool $readonly
*
* @return $this
*/
public function setReadonly($readonly)
{
parent::setReadonly($readonly);
$this->getState()->Readonly = $readonly;
return $this;
}
/** /**
* @return ArrayList * @return ArrayList
*/ */
@ -1009,6 +1029,9 @@ class GridField extends FormField
} }
if ($request->getHeader('X-Pjax') === 'CurrentField') { if ($request->getHeader('X-Pjax') === 'CurrentField') {
if ($this->getState()->Readonly) {
$this->performDisabledTransformation();
}
return $this->FieldHolder(); return $this->FieldHolder();
} }