Merge common functionality in to FormModelWidget
This commit is contained in:
parent
f45692ce84
commit
23f4d40ef2
|
|
@ -121,16 +121,4 @@ abstract class FormWidgetBase extends WidgetBase
|
|||
return $this->formField->getValueFromData($this->data ?: $this->model, $defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the final model and attribute name of
|
||||
* a nested HTML array attribute.
|
||||
* Eg: list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom);
|
||||
* @param string $attribute.
|
||||
* @return array
|
||||
*/
|
||||
public function resolveModelAttribute($attribute)
|
||||
{
|
||||
return $this->formField->resolveModelAttribute($this->model, $attribute);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +1,16 @@
|
|||
<?php namespace Backend\FormWidgets;
|
||||
|
||||
use Str;
|
||||
use Lang;
|
||||
use Input;
|
||||
use Request;
|
||||
use Response;
|
||||
use Validator;
|
||||
use System\Models\File;
|
||||
use ApplicationException;
|
||||
use Backend\Classes\FormField;
|
||||
use Backend\Classes\FormWidgetBase;
|
||||
use Backend\Controllers\Files as FilesController;
|
||||
use October\Rain\Filesystem\Definitions as FileDefinitions;
|
||||
use ApplicationException;
|
||||
use ValidationException;
|
||||
use Exception;
|
||||
|
||||
|
|
@ -30,6 +29,8 @@ use Exception;
|
|||
*/
|
||||
class FileUpload extends FormWidgetBase
|
||||
{
|
||||
use \Backend\Traits\FormModelWidget;
|
||||
|
||||
//
|
||||
// Configurable properties
|
||||
//
|
||||
|
|
@ -259,36 +260,6 @@ class FileUpload extends FormWidgetBase
|
|||
return implode(',', $types);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value as a relation object from the model,
|
||||
* supports nesting via HTML array.
|
||||
* @return Relation
|
||||
*/
|
||||
protected function getRelationObject()
|
||||
{
|
||||
list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom);
|
||||
|
||||
if (!$model->hasRelation($attribute)) {
|
||||
throw new ApplicationException(Lang::get('backend::lang.model.missing_relation', [
|
||||
'class' => get_class($model),
|
||||
'relation' => $attribute
|
||||
]));
|
||||
}
|
||||
|
||||
return $model->{$attribute}();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value as a relation type from the model,
|
||||
* supports nesting via HTML array.
|
||||
* @return Relation
|
||||
*/
|
||||
protected function getRelationType()
|
||||
{
|
||||
list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom);
|
||||
return $model->getRelationType($attribute);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a file attachment.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -22,6 +22,8 @@ use Backend\Classes\FormWidgetBase;
|
|||
*/
|
||||
class RecordFinder extends FormWidgetBase
|
||||
{
|
||||
use \Backend\Traits\FormModelWidget;
|
||||
|
||||
//
|
||||
// Configurable properties
|
||||
//
|
||||
|
|
@ -107,25 +109,6 @@ class RecordFinder extends FormWidgetBase
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the model of a relation type,
|
||||
* supports nesting via HTML array.
|
||||
* @return Relation
|
||||
*/
|
||||
protected function getRelationModel()
|
||||
{
|
||||
list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom);
|
||||
|
||||
if (!$model->hasRelation($attribute)) {
|
||||
throw new ApplicationException(Lang::get('backend::lang.model.missing_relation', [
|
||||
'class' => get_class($model),
|
||||
'relation' => $attribute
|
||||
]));
|
||||
}
|
||||
|
||||
return $model->makeRelation($attribute);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ use Illuminate\Database\Eloquent\Relations\Relation as RelationBase;
|
|||
*/
|
||||
class Relation extends FormWidgetBase
|
||||
{
|
||||
use \Backend\Traits\FormModelWidget;
|
||||
|
||||
//
|
||||
// Configurable properties
|
||||
//
|
||||
|
|
@ -161,25 +163,4 @@ class Relation extends FormWidgetBase
|
|||
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the value as a relation object from the model,
|
||||
* supports nesting via HTML array.
|
||||
* @return Relation
|
||||
*/
|
||||
protected function getRelationObject()
|
||||
{
|
||||
list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom);
|
||||
|
||||
if (!$model->hasRelation($attribute)) {
|
||||
throw new ApplicationException(Lang::get('backend::lang.model.missing_relation', [
|
||||
'class' => get_class($model),
|
||||
'relation' => $attribute
|
||||
]));
|
||||
}
|
||||
|
||||
return $model->{$attribute}();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,93 @@
|
|||
<?php namespace Backend\Traits;
|
||||
|
||||
use Lang;
|
||||
use ApplicationException;
|
||||
|
||||
/**
|
||||
* Form Model Widget Trait
|
||||
*
|
||||
* Special logic for for form widgets that use a database stored model.
|
||||
*
|
||||
* @package october\backend
|
||||
* @author Alexey Bobkov, Samuel Georges
|
||||
*/
|
||||
|
||||
trait FormModelWidget
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns the final model and attribute name of
|
||||
* a nested HTML array attribute.
|
||||
* Eg: list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom);
|
||||
* @param string $attribute.
|
||||
* @return array
|
||||
*/
|
||||
public function resolveModelAttribute($attribute)
|
||||
{
|
||||
return $this->formField->resolveModelAttribute($this->model, $attribute);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the model of a relation type,
|
||||
* supports nesting via HTML array.
|
||||
* @return Relation
|
||||
*/
|
||||
protected function getRelationModel()
|
||||
{
|
||||
list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom);
|
||||
|
||||
if (!$model) {
|
||||
throw new ApplicationException(Lang::get('backend::lang.model.missing_relation', [
|
||||
'class' => get_class($this->model),
|
||||
'relation' => $this->valueFrom
|
||||
]));
|
||||
}
|
||||
|
||||
if (!$model->hasRelation($attribute)) {
|
||||
throw new ApplicationException(Lang::get('backend::lang.model.missing_relation', [
|
||||
'class' => get_class($model),
|
||||
'relation' => $attribute
|
||||
]));
|
||||
}
|
||||
|
||||
return $model->makeRelation($attribute);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value as a relation object from the model,
|
||||
* supports nesting via HTML array.
|
||||
* @return Relation
|
||||
*/
|
||||
protected function getRelationObject()
|
||||
{
|
||||
list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom);
|
||||
|
||||
if (!$model) {
|
||||
throw new ApplicationException(Lang::get('backend::lang.model.missing_relation', [
|
||||
'class' => get_class($this->model),
|
||||
'relation' => $this->valueFrom
|
||||
]));
|
||||
}
|
||||
|
||||
if (!$model->hasRelation($attribute)) {
|
||||
throw new ApplicationException(Lang::get('backend::lang.model.missing_relation', [
|
||||
'class' => get_class($model),
|
||||
'relation' => $attribute
|
||||
]));
|
||||
}
|
||||
|
||||
return $model->{$attribute}();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value as a relation type from the model,
|
||||
* supports nesting via HTML array.
|
||||
* @return Relation
|
||||
*/
|
||||
protected function getRelationType()
|
||||
{
|
||||
list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom);
|
||||
return $model->getRelationType($attribute);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue