Skip to content
This repository
Browse code

ENHANCEMENT Anonymous function support in GridField->setFieldFormatti…

…ng()
  • Loading branch information...
commit a393d3937b965d45449639807ae9c577c75a745e 1 parent 0ef5d0b
Ingo Schommer authored April 17, 2012
3  forms/gridfield/GridField.php
@@ -195,7 +195,8 @@ public function setFieldCasting($casting) {
195 195
 	/**
196 196
 	 * Specify custom formatting for fields, e.g. to render a link instead of pure text.
197 197
 	 * Caution: Make sure to escape special php-characters like in a normal php-statement.
198  
-	 * Example:	"myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'
  198
+	 * Example:	"myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'.
  199
+	 * Alternatively, pass a anonymous function, which takes one parameter: The list item.
199 200
 	 *
200 201
 	 * @param array $casting
201 202
 	 * @todo refactor this into GridFieldComponent
15  forms/gridfield/GridFieldDataColumns.php
@@ -95,11 +95,16 @@ protected function formatValue($gridField, $item, $fieldName, $value) {
95 95
 			return $value;
96 96
 		}
97 97
 
98  
-		$format = str_replace('$value', "__VAL__", $gridField->FieldFormatting[$fieldName]);
99  
-		$format = preg_replace('/\$([A-Za-z0-9-_]+)/', '$item->$1', $format);
100  
-		$format = str_replace('__VAL__', '$value', $format);
101  
-		eval('$value = "' . $format . '";');
102  
-		return $value;
  98
+		$spec = $gridField->FieldFormatting[$fieldName];
  99
+		if(is_callable($spec)) {
  100
+			return $spec($item);
  101
+		} else {
  102
+			$format = str_replace('$value', "__VAL__", $spec);
  103
+			$format = preg_replace('/\$([A-Za-z0-9-_]+)/', '$item->$1', $format);
  104
+			$format = str_replace('__VAL__', '$value', $format);
  105
+			eval('$value = "' . $format . '";');
  106
+			return $value;
  107
+		}
103 108
 	}
104 109
 	
105 110
 	/**

0 notes on commit a393d39

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