diff --git a/modules/backend/classes/FormField.php b/modules/backend/classes/FormField.php index 67b2fdacc..c67b6c5c6 100644 --- a/modules/backend/classes/FormField.php +++ b/modules/backend/classes/FormField.php @@ -395,4 +395,54 @@ class FormField return Str::evalHtmlId($id); } + + /** + * Returns this fields value from a supplied data set, which can be + * an array or a model or another generic collection. + * @param mixed $data + * @return mixed + */ + public function getValueFromData($data, $default = null) + { + $fieldName = $this->fieldName; + + /* + * Array field name, eg: field[key][key2][key3] + */ + $keyParts = Str::evalHtmlArray($fieldName); + $lastField = end($keyParts); + $result = $data; + + /* + * Loop the field key parts and build a value. + * To support relations only the last field should return the + * relation value, all others will look up the relation object as normal. + */ + foreach ($keyParts as $key) { + + if ($result instanceof Model && $result->hasRelation($key)) { + if ($key == $lastField) { + $result = $result->getRelationValue($key) ?: $default; + } + else { + $result = $result->{$key}; + } + } + elseif (is_array($result)) { + if (!array_key_exists($key, $result)) { + return $default; + } + $result = $result[$key]; + } + else { + if (!isset($result->{$key})) { + return $default; + } + $result = $result->{$key}; + } + + } + + return $result; + } } diff --git a/modules/backend/classes/FormWidgetBase.php b/modules/backend/classes/FormWidgetBase.php index 1fb2be4b9..da539c723 100644 --- a/modules/backend/classes/FormWidgetBase.php +++ b/modules/backend/classes/FormWidgetBase.php @@ -103,13 +103,7 @@ abstract class FormWidgetBase extends WidgetBase */ public function getLoadValue() { - list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom); - - if (!is_null($model)) { - return $model->{$attribute}; - } - - return null; + return $this->formField->getValueFromData($this->model); } /** diff --git a/modules/backend/formwidgets/RecordFinder.php b/modules/backend/formwidgets/RecordFinder.php index a2b57e5e8..3e94b77fb 100644 --- a/modules/backend/formwidgets/RecordFinder.php +++ b/modules/backend/formwidgets/RecordFinder.php @@ -142,7 +142,6 @@ class RecordFinder extends FormWidgetBase */ public function prepareVars() { - // This should be a relation and return a Model $this->relationModel = $this->getLoadValue(); $this->vars['value'] = $this->getKeyValue(); @@ -170,6 +169,20 @@ class RecordFinder extends FormWidgetBase return strlen($value) ? $value : null; } + /** + * {@inheritDoc} + */ + public function getLoadValue() + { + list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom); + + if (!is_null($model)) { + return $model->{$attribute}; + } + + return null; + } + public function getKeyValue() { if (!$this->relationModel) { diff --git a/modules/backend/widgets/Form.php b/modules/backend/widgets/Form.php index 445bf6eae..b9b388155 100644 --- a/modules/backend/widgets/Form.php +++ b/modules/backend/widgets/Form.php @@ -709,49 +709,11 @@ class Form extends WidgetBase $field = $this->fields[$field]; } - $fieldName = $field->fieldName; $defaultValue = (!$this->model->exists && $field->defaults !== '') ? $field->defaults : null; - /* - * Array field name, eg: field[key][key2][key3] - */ - $keyParts = Str::evalHtmlArray($fieldName); - $lastField = end($keyParts); - $result = $this->data; - - /* - * Loop the field key parts and build a value. - * To support relations only the last field should return the - * relation value, all others will look up the relation object as normal. - */ - foreach ($keyParts as $key) { - - if ($result instanceof Model && $result->hasRelation($key)) { - if ($key == $lastField) { - $result = $result->getRelationValue($key) ?: $defaultValue; - } - else { - $result = $result->{$key}; - } - } - elseif (is_array($result)) { - if (!array_key_exists($key, $result)) { - return $defaultValue; - } - $result = $result[$key]; - } - else { - if (!isset($result->{$key})) { - return $defaultValue; - } - $result = $result->{$key}; - } - - } - - return $result; + return $field->getValueFromData($this->data, $defaultValue); } /**