From 82c2787f39834b972da2b25bf93b08a91d19a640 Mon Sep 17 00:00:00 2001 From: flynsarmy Date: Fri, 9 Oct 2015 13:51:33 +1000 Subject: [PATCH] Add support for default values in form widgets --- modules/backend/classes/FormWidgetBase.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/modules/backend/classes/FormWidgetBase.php b/modules/backend/classes/FormWidgetBase.php index 716b84dd0..f63d32d64 100644 --- a/modules/backend/classes/FormWidgetBase.php +++ b/modules/backend/classes/FormWidgetBase.php @@ -113,7 +113,19 @@ abstract class FormWidgetBase extends WidgetBase */ public function getLoadValue() { - return $this->formField->getValueFromData($this->data ?: $this->model); + $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; + } + } + + return $this->formField->getValueFromData($this->data ?: $this->model, $defaultValue); } /**