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 = [
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();
}