From 74343af4c6321f82d3115afa49cdbaa1ca979309 Mon Sep 17 00:00:00 2001 From: Jake Bentvelzen Date: Mon, 25 Jan 2016 15:10:35 +1100 Subject: [PATCH] Updated GridFieldEditableColumns to have nicer defaults, so you can just add it in and it'll "just work" with your 'summary_fields' data. --- code/GridFieldEditableColumns.php | 54 +++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/code/GridFieldEditableColumns.php b/code/GridFieldEditableColumns.php index aede083..aee407f 100644 --- a/code/GridFieldEditableColumns.php +++ b/code/GridFieldEditableColumns.php @@ -29,12 +29,37 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements } $fields = $this->getForm($grid, $record)->Fields(); - $value = $grid->getDataFieldValue($record, $col); - $rel = (strpos($col,'.') === false); // field references a relation value - $field = ($rel) ? clone $fields->fieldByName($col) : new ReadonlyField($col); - if(!$field) { - throw new Exception("Could not find the field '$col'"); + if (!$this->displayFields) + { + // If setDisplayFields() not used, utilize $summary_fields + // in a way similar to base class + $colRelation = explode('.', $col); + $value = $grid->getDataFieldValue($record, $colRelation[0]); + $field = $fields->fieldByName($colRelation[0]); + if (!$field || $field->isReadonly() || $field->isDisabled()) { + return parent::getColumnContent($grid, $record, $col); + } + + // Ensure this field is available to edit on the record + // (ie. Maybe its readonly due to certain circumstances, or removed and not editable) + $cmsFields = $record->getCMSFields(); + $cmsField = $cmsFields->dataFieldByName($colRelation[0]); + if (!$cmsField || $cmsField->isReadonly() || $cmsField->isDisabled()) + { + return parent::getColumnContent($grid, $record, $col); + } + $field = clone $field; + } + else + { + $value = $grid->getDataFieldValue($record, $col); + $rel = (strpos($col,'.') === false); // field references a relation value + $field = ($rel) ? clone $fields->fieldByName($col) : new ReadonlyField($col); + + if(!$field) { + throw new Exception("Could not find the field '$col'"); + } } if(array_key_exists($col, $this->fieldCasting)) { @@ -163,7 +188,24 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements } if(!$field) { - if($class && $obj = singleton($class)->dbObject($col)) { + if (!$this->displayFields) + { + // If setDisplayFields() not used, utilize $summary_fields + // in a way similar to base class + // + // Allows use of 'MyBool.Nice' and 'MyHTML.NoHTML' so that + // GridFields not using inline editing still look good or + // revert to looking good in cases where the field isn't + // available or is readonly + // + $colRelation = explode('.', $col); + if($class && $obj = singleton($class)->dbObject($colRelation[0])) { + $field = $obj->scaffoldFormField(); + } else { + $field = new ReadonlyField($colRelation[0]); + } + } + else if($class && $obj = singleton($class)->dbObject($col)) { $field = $obj->scaffoldFormField(); } else { $field = new ReadonlyField($col);