Skip to content
This repository
Browse code

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
Sam Minnée authored May 29, 2012
29  forms/gridfield/GridField.php
@@ -56,6 +56,11 @@ class GridField extends FormField {
56 56
 	 * @var array
57 57
 	 */
58 58
 	protected $columnDispatch = null;
  59
+	
  60
+	/**
  61
+	 * Map of callbacks for custom data fields
  62
+	 */
  63
+	protected $customDataFields = array();
59 64
 
60 65
 	protected $name = '';
61 66
 
@@ -411,6 +416,30 @@ public function getColumnContent($record, $column) {
411 416
 			throw new InvalidArgumentException("Bad column '$column'");
412 417
 		}
413 418
 	}
  419
+	
  420
+	/**
  421
+	 * Add additional calculated data fields to be used on this GridField
  422
+	 * @param array $fields a map of fieldname to callback.  The callback will bed passed the record as an argument.
  423
+	 */
  424
+	public function addDataFields($fields) {
  425
+		if($this->customDataFields) $this->customDataFields = array_merge($this->customDataFields, $fields);
  426
+		else $this->customDataFields = $fields;		
  427
+	}
  428
+	
  429
+	/**
  430
+	 * Get the value of a named field  on the given record.
  431
+	 * Use of this method ensures that any special rules around the data for this gridfield are followed.
  432
+	 */
  433
+	public function getDataFieldValue($record, $fieldName) {
  434
+		// Custom callbacks
  435
+		if(isset($this->customDataFields[$fieldName])) {
  436
+			$callback = $this->customDataFields[$fieldName];
  437
+			return $callback($record);
  438
+		}
  439
+		
  440
+		// Default implementation
  441
+		return $record->relField($fieldName);
  442
+	}
414 443
 
415 444
 	/**
416 445
 	 * Get extra columns attributes used as HTML attributes
23  forms/gridfield/GridFieldDataColumns.php
@@ -118,11 +118,18 @@ public function getFieldFormatting() {
118 118
 	 * @return string HTML for the column. Return NULL to skip.
119 119
 	 */
120 120
 	public function getColumnContent($gridField, $record, $columnName) {
  121
+		// Find the data column for the given named column
  122
+		$columns = $this->getDisplayFields($gridField);
  123
+		$columnInfo = $columns[$columnName];
  124
+		
  125
+		// Allow callbacks
  126
+		if(is_array($columnInfo) && isset($columnInfo['callback'])) {
  127
+			$method = $columnInfo['callback'];
  128
+			$value = Convert::raw2xml($method($record));
  129
+		
121 130
 		// This supports simple FieldName syntax
122  
-		if(strpos($columnName, '.') === false) {
123  
-			$value = $record->XML_val($columnName);
124 131
 		} else {
125  
-			$value = $this->getValueFromRelation($record, $columnName);
  132
+			$value = Convert::raw2xml($gridField->getDataFieldValue($record, $columnName));
126 133
 		}
127 134
 
128 135
 		$value = $this->castValue($gridField, $columnName, $value);
@@ -154,8 +161,16 @@ public function getColumnAttributes($gridField, $record, $columnName) {
154 161
 	 */
155 162
 	public function getColumnMetadata($gridField, $column) {
156 163
 		$columns = $this->getDisplayFields($gridField);
  164
+		
  165
+		$title = null;
  166
+		if(is_string($columns[$column])) {
  167
+			$title = $columns[$column];
  168
+		} else if(is_array($columns[$column]) && isset($columns[$column]['title'])) {
  169
+			$title = $columns[$column]['title'];
  170
+		}
  171
+		
157 172
 		return array(
158  
-			'title' => $columns[$column],
  173
+			'title' => $title,
159 174
 		);
160 175
 	}
161 176
 	
2  forms/gridfield/GridFieldExportButton.php
@@ -138,7 +138,7 @@ function generateExportFileData($gridField) {
138 138
 
139 139
 					$value = $columnHeader($relObj);
140 140
 				} else {
141  
-					$value = $item->relField($columnSource);
  141
+					$value = $gridField->getDataFieldValue($item, $columnSource);
142 142
 				}
143 143
 
144 144
 				$value = str_replace(array("\r", "\n"), "\n", $value);
2  forms/gridfield/GridFieldPrintButton.php
@@ -115,7 +115,7 @@ function generatePrintData($gridField) {
115 115
 		foreach($items as $item) {
116 116
 			$itemRow = new ArrayList();
117 117
 			foreach($printColumns as $field => $label) {
118  
-				$value = $item->relField($field);
  118
+				$value = $gridField->getDataFieldValue($item, $field);
119 119
 				$itemRow->push(
120 120
 					new ArrayData(array(
121 121
 						"CellString" => $value,

0 notes on commit 70d5ffe

Please sign in to comment.
Something went wrong with that request. Please try again.