Add support for \Path\To\Class::staticMethodName for defining field options.

Related: 95d0b61a29
This commit is contained in:
Luke Towers 2020-09-10 12:12:46 -06:00
parent 619be11d23
commit b407f26e02
2 changed files with 18 additions and 0 deletions

View File

@ -9,6 +9,7 @@ return [
'invalid_type' => 'Invalid field type used :type.',
'options_method_invalid_model' => "The attribute ':field' does not resolve to a valid model. Try specifying the options method for model class :model explicitly.",
'options_method_not_exists' => "The model class :model must define a method :method() returning options for the ':field' form field.",
'options_static_method_invalid_value' => "The static method ':method()' on :class did not return a valid options array.",
'colors_method_not_exists' => "The model class :model must define a method :method() returning html color HEX codes for the ':field' form field.",
],
'widget' => [

View File

@ -1283,6 +1283,7 @@ class Form extends WidgetBase
{
/*
* Advanced usage, supplied options are callable
* [\Path\To\Class, methodName]
*/
if (is_array($fieldOptions) && is_callable($fieldOptions)) {
$fieldOptions = call_user_func($fieldOptions, $this, $field);
@ -1328,6 +1329,22 @@ class Form extends WidgetBase
* Field options are an explicit method reference
*/
elseif (is_string($fieldOptions)) {
// \Path\To\Class::staticMethodOptions
if (str_contains($fieldOptions, '::')) {
$options = explode('::', $fieldOptions);
if (count($options) === 2 && class_exists($options[0]) && method_exists($options[0], $options[1])) {
$result = $options[0]::{$options[1]}();
if (!is_array($result)) {
throw new ApplicationException(Lang::get('backend::lang.field.options_static_method_invalid_value', [
'class' => $options[0],
'method' => $options[1]
]));
}
return $result;
}
}
// $model->{$fieldOptions}()
if (!$this->objectMethodExists($this->model, $fieldOptions)) {
throw new ApplicationException(Lang::get('backend::lang.field.options_method_not_exists', [
'model' => get_class($this->model),