ENHANCEMENT Anonymous function support in GridField->setFieldFormatting()

This commit is contained in:
Ingo Schommer 2012-04-17 17:11:43 +02:00
parent 0ef5d0b84f
commit a393d3937b
2 changed files with 12 additions and 6 deletions

View File

@ -195,7 +195,8 @@ class GridField extends FormField {
/**
* 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 File

@ -95,11 +95,16 @@ class GridFieldDataColumns implements GridField_ColumnProvider {
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;
}
}
/**