mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
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:
parent
780f2d2b16
commit
70d5ffefdd
@ -57,6 +57,11 @@ class GridField extends FormField {
|
|||||||
*/
|
*/
|
||||||
protected $columnDispatch = null;
|
protected $columnDispatch = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map of callbacks for custom data fields
|
||||||
|
*/
|
||||||
|
protected $customDataFields = array();
|
||||||
|
|
||||||
protected $name = '';
|
protected $name = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -412,6 +417,30 @@ class GridField extends FormField {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
* Get extra columns attributes used as HTML attributes
|
||||||
*
|
*
|
||||||
|
@ -118,11 +118,18 @@ class GridFieldDataColumns implements GridField_ColumnProvider {
|
|||||||
* @return string HTML for the column. Return NULL to skip.
|
* @return string HTML for the column. Return NULL to skip.
|
||||||
*/
|
*/
|
||||||
public function getColumnContent($gridField, $record, $columnName) {
|
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
|
// This supports simple FieldName syntax
|
||||||
if(strpos($columnName, '.') === false) {
|
|
||||||
$value = $record->XML_val($columnName);
|
|
||||||
} else {
|
} else {
|
||||||
$value = $this->getValueFromRelation($record, $columnName);
|
$value = Convert::raw2xml($gridField->getDataFieldValue($record, $columnName));
|
||||||
}
|
}
|
||||||
|
|
||||||
$value = $this->castValue($gridField, $columnName, $value);
|
$value = $this->castValue($gridField, $columnName, $value);
|
||||||
@ -154,8 +161,16 @@ class GridFieldDataColumns implements GridField_ColumnProvider {
|
|||||||
*/
|
*/
|
||||||
public function getColumnMetadata($gridField, $column) {
|
public function getColumnMetadata($gridField, $column) {
|
||||||
$columns = $this->getDisplayFields($gridField);
|
$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(
|
return array(
|
||||||
'title' => $columns[$column],
|
'title' => $title,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,7 +138,7 @@ class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
|
|
||||||
$value = $columnHeader($relObj);
|
$value = $columnHeader($relObj);
|
||||||
} else {
|
} else {
|
||||||
$value = $item->relField($columnSource);
|
$value = $gridField->getDataFieldValue($item, $columnSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
$value = str_replace(array("\r", "\n"), "\n", $value);
|
$value = str_replace(array("\r", "\n"), "\n", $value);
|
||||||
|
@ -115,7 +115,7 @@ class GridFieldPrintButton implements GridField_HTMLProvider, GridField_ActionPr
|
|||||||
foreach($items as $item) {
|
foreach($items as $item) {
|
||||||
$itemRow = new ArrayList();
|
$itemRow = new ArrayList();
|
||||||
foreach($printColumns as $field => $label) {
|
foreach($printColumns as $field => $label) {
|
||||||
$value = $item->relField($field);
|
$value = $gridField->getDataFieldValue($item, $field);
|
||||||
$itemRow->push(
|
$itemRow->push(
|
||||||
new ArrayData(array(
|
new ArrayData(array(
|
||||||
"CellString" => $value,
|
"CellString" => $value,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user