From ef22ac934d773ba62b44ed460f73205c34ca0127 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 17 Oct 2015 10:16:49 +1100 Subject: [PATCH] DRY up code from #1504 --- modules/backend/classes/FormField.php | 75 +++++++++++++++------- modules/backend/classes/FormWidgetBase.php | 14 +--- modules/backend/widgets/Form.php | 14 +--- 3 files changed, 59 insertions(+), 44 deletions(-) diff --git a/modules/backend/classes/FormField.php b/modules/backend/classes/FormField.php index 4ef1aeac4..4adf2e571 100644 --- a/modules/backend/classes/FormField.php +++ b/modules/backend/classes/FormField.php @@ -543,12 +543,65 @@ class FormField * Returns this fields value from a supplied data set, which can be * an array or a model or another generic collection. * @param mixed $data + * @param mixed $default * @return mixed */ public function getValueFromData($data, $default = null) { $fieldName = $this->valueFrom ?: $this->fieldName; + return $this->getFieldNameFromData($fieldName, $data, $default); + } + /** + * Returns the default value for this field, the supplied data is used + * to source data when defaultFrom is specified. + * @param mixed $data + * @return mixed + */ + public function getDefaultFromData($data) + { + if ($this->defaultFrom) { + return $this->getFieldNameFromData($this->defaultFrom, $data); + } + + if ($this->defaults !== '') { + return $this->defaults; + } + + return null; + } + + /** + * Returns the final model and attribute name of a nested attribute. + * Eg: list($model, $attribute) = $this->resolveAttribute('person[phone]'); + * @param string $attribute. + * @return array + */ + public function resolveModelAttribute($model, $attribute = null) + { + if ($attribute === null) { + $attribute = $this->valueFrom ?: $this->fieldName; + } + + $parts = is_array($attribute) ? $attribute : HtmlHelper::nameToArray($attribute); + $last = array_pop($parts); + + foreach ($parts as $part) { + $model = $model->{$part}; + } + + return [$model, $last]; + } + + /** + * Internal method to extract the value of a field name from a data set. + * @param string $fieldName + * @param mixed $data + * @param mixed $default + * @return mixed + */ + protected function getFieldNameFromData($fieldName, $data, $default = null) + { /* * Array field name, eg: field[key][key2][key3] */ @@ -588,26 +641,4 @@ class FormField return $result; } - - /** - * Returns the final model and attribute name of a nested attribute. - * Eg: list($model, $attribute) = $this->resolveAttribute('person[phone]'); - * @param string $attribute. - * @return array - */ - public function resolveModelAttribute($model, $attribute = null) - { - if ($attribute === null) { - $attribute = $this->valueFrom ?: $this->fieldName; - } - - $parts = is_array($attribute) ? $attribute : HtmlHelper::nameToArray($attribute); - $last = array_pop($parts); - - foreach ($parts as $part) { - $model = $model->{$part}; - } - - return [$model, $last]; - } } diff --git a/modules/backend/classes/FormWidgetBase.php b/modules/backend/classes/FormWidgetBase.php index f63d32d64..97182613b 100644 --- a/modules/backend/classes/FormWidgetBase.php +++ b/modules/backend/classes/FormWidgetBase.php @@ -113,17 +113,9 @@ abstract class FormWidgetBase extends WidgetBase */ public function getLoadValue() { - $defaultValue = null; - - if (!$this->model->exists) { - if ($this->formField->defaultFrom) { - list($model, $attribute) = $this->formField->resolveModelAttribute($this->model, $this->formField->defaultFrom); - $defaultValue = $model->{$attribute}; - } - elseif ($this->formField->defaults !== '') { - $defaultValue = $this->formField->defaults; - } - } + $defaultValue = !$this->model->exists + ? $this->formField->getDefaultFromData($this->data ?: $this->model) + : null; return $this->formField->getValueFromData($this->data ?: $this->model, $defaultValue); } diff --git a/modules/backend/widgets/Form.php b/modules/backend/widgets/Form.php index f0d84e2d6..f5a3eb3df 100644 --- a/modules/backend/widgets/Form.php +++ b/modules/backend/widgets/Form.php @@ -775,17 +775,9 @@ class Form extends WidgetBase $field = $this->allFields[$field]; } - $defaultValue = null; - - if (!$this->model->exists) { - if ($field->defaultFrom) { - list($model, $attribute) = $field->resolveModelAttribute($this->model, $field->defaultFrom); - $defaultValue = $model->{$attribute}; - } - elseif ($field->defaults !== '') { - $defaultValue = $field->defaults; - } - } + $defaultValue = !$this->model->exists + ? $field->getDefaultFromData($this->data) + : null; return $field->getValueFromData($this->data, $defaultValue); }