Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit 70d5ffefdd12eb3e69f406cf95fa90a8abb27d6d 1 parent 780f2d2
@sminnee sminnee authored
View
29 forms/gridfield/GridField.php
@@ -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 @@ public function getColumnContent($record, $column) {
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
23 forms/gridfield/GridFieldDataColumns.php
@@ -118,11 +118,18 @@ public function getFieldFormatting() {
* @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 @@ public function getColumnAttributes($gridField, $record, $columnName) {
*/
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
2  forms/gridfield/GridFieldExportButton.php
@@ -138,7 +138,7 @@ function generateExportFileData($gridField) {
$value = $columnHeader($relObj);
} else {
- $value = $item->relField($columnSource);
+ $value = $gridField->getDataFieldValue($item, $columnSource);
}
$value = str_replace(array("\r", "\n"), "\n", $value);
View
2  forms/gridfield/GridFieldPrintButton.php
@@ -115,7 +115,7 @@ function generatePrintData($gridField) {
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,
Please sign in to comment.
Something went wrong with that request. Please try again.