API CHANGE: Added GridField::addDataFields() to allow the definition of custom callbacks to be used by all GridField components.

API CHANGE: Added GridField::getDataFieldValue() to encapsulate field lookup for all components.
API CHANGE: Allow 'callback' key to be specified in a GridFieldDataColumn column info.  In this case, title should be put as the 'title' key of a map rather than simply the column info.
This commit is contained in:
Sam Minnee 2012-05-29 10:06:30 +12:00
parent 780f2d2b16
commit 70d5ffefdd
4 changed files with 50 additions and 6 deletions

View File

@ -56,6 +56,11 @@ class GridField extends FormField {
* @var array
*/
protected $columnDispatch = null;
/**
* Map of callbacks for custom data fields
*/
protected $customDataFields = array();
protected $name = '';
@ -411,6 +416,30 @@ class GridField extends FormField {
throw new InvalidArgumentException("Bad column '$column'");
}
}
/**
* Add additional calculated data fields to be used on this GridField
* @param array $fields a map of fieldname to callback. The callback will bed passed the record as an argument.
*/
public function addDataFields($fields) {
if($this->customDataFields) $this->customDataFields = array_merge($this->customDataFields, $fields);
else $this->customDataFields = $fields;
}
/**
* Get the value of a named field on the given record.
* Use of this method ensures that any special rules around the data for this gridfield are followed.
*/
public function getDataFieldValue($record, $fieldName) {
// Custom callbacks
if(isset($this->customDataFields[$fieldName])) {
$callback = $this->customDataFields[$fieldName];
return $callback($record);
}
// Default implementation
return $record->relField($fieldName);
}
/**
* Get extra columns attributes used as HTML attributes

View File

@ -118,11 +118,18 @@ class GridFieldDataColumns implements GridField_ColumnProvider {
* @return string HTML for the column. Return NULL to skip.
*/
public function getColumnContent($gridField, $record, $columnName) {
// Find the data column for the given named column
$columns = $this->getDisplayFields($gridField);
$columnInfo = $columns[$columnName];
// Allow callbacks
if(is_array($columnInfo) && isset($columnInfo['callback'])) {
$method = $columnInfo['callback'];
$value = Convert::raw2xml($method($record));
// This supports simple FieldName syntax
if(strpos($columnName, '.') === false) {
$value = $record->XML_val($columnName);
} else {
$value = $this->getValueFromRelation($record, $columnName);
$value = Convert::raw2xml($gridField->getDataFieldValue($record, $columnName));
}
$value = $this->castValue($gridField, $columnName, $value);
@ -154,8 +161,16 @@ class GridFieldDataColumns implements GridField_ColumnProvider {
*/
public function getColumnMetadata($gridField, $column) {
$columns = $this->getDisplayFields($gridField);
$title = null;
if(is_string($columns[$column])) {
$title = $columns[$column];
} else if(is_array($columns[$column]) && isset($columns[$column]['title'])) {
$title = $columns[$column]['title'];
}
return array(
'title' => $columns[$column],
'title' => $title,
);
}

View File

@ -138,7 +138,7 @@ class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionP
$value = $columnHeader($relObj);
} else {
$value = $item->relField($columnSource);
$value = $gridField->getDataFieldValue($item, $columnSource);
}
$value = str_replace(array("\r", "\n"), "\n", $value);

View File

@ -115,7 +115,7 @@ class GridFieldPrintButton implements GridField_HTMLProvider, GridField_ActionPr
foreach($items as $item) {
$itemRow = new ArrayList();
foreach($printColumns as $field => $label) {
$value = $item->relField($field);
$value = $gridField->getDataFieldValue($item, $field);
$itemRow->push(
new ArrayData(array(
"CellString" => $value,