mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
ENHANCEMENT Anonymous function support in GridField->setFieldFormatting()
This commit is contained in:
parent
0ef5d0b84f
commit
a393d3937b
@ -195,7 +195,8 @@ class GridField extends FormField {
|
|||||||
/**
|
/**
|
||||||
* Specify custom formatting for fields, e.g. to render a link instead of pure text.
|
* 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.
|
* 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
|
* @param array $casting
|
||||||
* @todo refactor this into GridFieldComponent
|
* @todo refactor this into GridFieldComponent
|
||||||
|
@ -95,12 +95,17 @@ class GridFieldDataColumns implements GridField_ColumnProvider {
|
|||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
$format = str_replace('$value', "__VAL__", $gridField->FieldFormatting[$fieldName]);
|
$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 = preg_replace('/\$([A-Za-z0-9-_]+)/', '$item->$1', $format);
|
||||||
$format = str_replace('__VAL__', '$value', $format);
|
$format = str_replace('__VAL__', '$value', $format);
|
||||||
eval('$value = "' . $format . '";');
|
eval('$value = "' . $format . '";');
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove values from a value using FieldEscape setter
|
* Remove values from a value using FieldEscape setter
|
||||||
|
Loading…
x
Reference in New Issue
Block a user