diff --git a/modules/backend/formwidgets/RecordFinder.php b/modules/backend/formwidgets/RecordFinder.php index c8f212126..61e54980c 100644 --- a/modules/backend/formwidgets/RecordFinder.php +++ b/modules/backend/formwidgets/RecordFinder.php @@ -1,5 +1,7 @@ useRelation && !class_exists($this->modelClass)) { + throw new ApplicationException(Lang::get('backend::lang.recordfinder.invalid_model_class', ['modelClass' => $this->modelClass])); + } + if (post('recordfinder_flag')) { $this->listWidget = $this->makeListWidget(); $this->listWidget->bindToController(); @@ -151,8 +177,13 @@ class RecordFinder extends FormWidgetBase public function onRefresh() { - list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom); - $model->{$attribute} = post($this->getFieldName()); + $value = post($this->getFieldName()); + if ($this->useRelation) { + list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom); + $model->{$attribute} = $value; + } else { + $this->formField->value = $value; + } $this->prepareVars(); return ['#'.$this->getId('container') => $this->makePartial('recordfinder')]; @@ -160,8 +191,12 @@ class RecordFinder extends FormWidgetBase public function onClearRecord() { - list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom); - $model->{$attribute} = null; + if ($this->useRelation) { + list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom); + $model->{$attribute} = null; + } else { + $this->formField->value = null; + } $this->prepareVars(); return ['#'.$this->getId('container') => $this->makePartial('recordfinder')]; @@ -209,13 +244,18 @@ class RecordFinder extends FormWidgetBase */ public function getLoadValue() { - list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom); + $value = null; - if ($model !== null) { - return $model->{$attribute}; + if ($this->useRelation) { + list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom); + if ($model !== null) { + $value = $model->{$attribute}; + } + } else { + $value = $this->modelClass::find(parent::getLoadValue()); } - return null; + return $value; } public function getKeyValue() @@ -263,7 +303,13 @@ class RecordFinder extends FormWidgetBase protected function makeListWidget() { $config = $this->makeConfig($this->getConfig('list')); - $config->model = $this->getRelationModel(); + + if ($this->useRelation) { + $config->model = $this->getRelationModel(); + } else { + $config->model = new $this->modelClass; + } + $config->alias = $this->alias . 'List'; $config->showSetup = false; $config->showCheckboxes = false; @@ -287,9 +333,11 @@ class RecordFinder extends FormWidgetBase }); } else { - $widget->bindEvent('list.extendQueryBefore', function ($query) { - $this->getRelationObject()->addDefinedConstraintsToQuery($query); - }); + if ($this->useRelation) { + $widget->bindEvent('list.extendQueryBefore', function ($query) { + $this->getRelationObject()->addDefinedConstraintsToQuery($query); + }); + } } return $widget; diff --git a/modules/backend/lang/en/lang.php b/modules/backend/lang/en/lang.php index 565eb2d44..7f6c642eb 100644 --- a/modules/backend/lang/en/lang.php +++ b/modules/backend/lang/en/lang.php @@ -299,6 +299,7 @@ return [ ], 'recordfinder' => [ 'find_record' => 'Find Record', + 'invalid_model_class' => 'The provided model class ":modelClass" for the recordfinder is invalid', 'cancel' => 'Cancel', ], 'pagelist' => [