Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ENHANCEMENT Anonymous function support in GridField->setFieldFormatti…

…ng()
  • Loading branch information...
commit a393d3937b965d45449639807ae9c577c75a745e 1 parent 0ef5d0b
@chillu chillu authored
View
3  forms/gridfield/GridField.php
@@ -195,7 +195,8 @@ public function setFieldCasting($casting) {
/**
* Specify custom formatting for fields, e.g. to render a link instead of pure text.
* Caution: Make sure to escape special php-characters like in a normal php-statement.
- * Example: "myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'
+ * Example: "myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'.
+ * Alternatively, pass a anonymous function, which takes one parameter: The list item.
*
* @param array $casting
* @todo refactor this into GridFieldComponent
View
15 forms/gridfield/GridFieldDataColumns.php
@@ -95,11 +95,16 @@ protected function formatValue($gridField, $item, $fieldName, $value) {
return $value;
}
- $format = str_replace('$value', "__VAL__", $gridField->FieldFormatting[$fieldName]);
- $format = preg_replace('/\$([A-Za-z0-9-_]+)/', '$item->$1', $format);
- $format = str_replace('__VAL__', '$value', $format);
- eval('$value = "' . $format . '";');
- return $value;
+ $spec = $gridField->FieldFormatting[$fieldName];
+ if(is_callable($spec)) {
+ return $spec($item);
+ } else {
+ $format = str_replace('$value', "__VAL__", $spec);
+ $format = preg_replace('/\$([A-Za-z0-9-_]+)/', '$item->$1', $format);
+ $format = str_replace('__VAL__', '$value', $format);
+ eval('$value = "' . $format . '";');
+ return $value;
+ }
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.