From 507bfab768005823738fa1e71542ef69c72b0b16 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Fri, 6 Mar 2015 20:37:05 +1100 Subject: [PATCH] !!! *BREAKING*: Form widget base class no longer takes a model as the 2nd argument, it should be passed as `model` in the configuration instead. --- CHANGELOG.md | 5 ++ modules/backend/classes/FormWidgetBase.php | 64 ++++++++++++++-------- modules/backend/classes/WidgetBase.php | 9 ++- modules/backend/widgets/Form.php | 6 +- modules/system/traits/ConfigMaker.php | 4 ++ 5 files changed, 57 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cebcdfc9..07a9804e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +* **Build 217** (2015-03-06) + - Added new form field widget called `repeater` for repeatable fields (see Backend > Forms docs). + - Made some UI improvements to the Rich Editor. + - Form widget base class no longer takes a model as the 2nd argument, it should be passed as `model` in the configuration instead. + * **Build 214** (2015-03-03) - Introduce `AjaxException` that allows response contents to be sent to framework.js and still be treated as an error. diff --git a/modules/backend/classes/FormWidgetBase.php b/modules/backend/classes/FormWidgetBase.php index 1a9a9b078..ce85cc594 100644 --- a/modules/backend/classes/FormWidgetBase.php +++ b/modules/backend/classes/FormWidgetBase.php @@ -11,26 +11,21 @@ use Str; */ abstract class FormWidgetBase extends WidgetBase { - /** - * @var FormField Object containing general form field information. - */ - public $formField; + + // + // Configurable properties + // /** - * @var string Form field name. - */ - public $fieldName; - - /** - * @var string Model attribute to get/set value from. - */ - public $valueFrom; - - /** - * @var mixed Model object. + * @var Model Form model object. */ public $model; + /** + * @var array Dataset containing field values, if none supplied model should be used. + */ + public $data; + /** * @var string Active session key, used for editing forms and deferred bindings. */ @@ -46,6 +41,25 @@ abstract class FormWidgetBase extends WidgetBase */ public $showLabels = true; + // + // Object properties + // + + /** + * @var FormField Object containing general form field information. + */ + protected $formField; + + /** + * @var string Form field name. + */ + protected $fieldName; + + /** + * @var string Model attribute to get/set value from. + */ + protected $valueFrom; + /** * Constructor * @param $controller Controller Active controller object. @@ -53,19 +67,21 @@ abstract class FormWidgetBase extends WidgetBase * @param $formField FormField Object containing general form field information. * @param $configuration array Configuration the relates to this widget. */ - public function __construct($controller, $model, $formField, $configuration = []) + public function __construct($controller, $formField, $configuration = []) { $this->formField = $formField; $this->fieldName = $formField->fieldName; $this->valueFrom = $formField->valueFrom; - $this->model = $model; - if (isset($configuration->sessionKey)) { - $this->sessionKey = $configuration->sessionKey; - } - if (isset($configuration->previewMode)) { - $this->previewMode = $configuration->previewMode; - } + $this->config = $this->makeConfig($configuration); + + $this->fillFromConfig([ + 'model', + 'data', + 'sessionKey', + 'previewMode', + 'showLabels' + ]); parent::__construct($controller, $configuration); } @@ -97,7 +113,7 @@ abstract class FormWidgetBase extends WidgetBase */ public function getLoadValue() { - return $this->formField->getValueFromData($this->model); + return $this->formField->getValueFromData($this->data ?: $this->model); } /** diff --git a/modules/backend/classes/WidgetBase.php b/modules/backend/classes/WidgetBase.php index 1f098e8f3..e71510cc3 100644 --- a/modules/backend/classes/WidgetBase.php +++ b/modules/backend/classes/WidgetBase.php @@ -52,14 +52,13 @@ abstract class WidgetBase $this->assetPath = $this->guessViewPath('/assets', true); /* - * Apply configuration values to a new config object. + * Apply configuration values to a new config object, if a parent + * consutrctor hasn't done it already. */ - if (!$configuration) { - $configuration = []; + if ($this->config === null) { + $this->config = $this->makeConfig($configuration); } - $this->config = $this->makeConfig($configuration); - /* * If no alias is set by the configuration. */ diff --git a/modules/backend/widgets/Form.php b/modules/backend/widgets/Form.php index 5fe67f221..abae743a9 100644 --- a/modules/backend/widgets/Form.php +++ b/modules/backend/widgets/Form.php @@ -679,6 +679,9 @@ class Form extends WidgetBase $widgetConfig = $this->makeConfig($field->config); $widgetConfig->alias = $this->alias . studly_case(Str::evalHtmlId($field->fieldName)); $widgetConfig->sessionKey = $this->getSessionKey(); + $widgetConfig->previewMode = $this->previewMode; + $widgetConfig->model = $this->model; + $widgetConfig->data = $this->data; $widgetName = $widgetConfig->widget; $widgetClass = $this->widgetManager->resolveFormWidget($widgetName); @@ -689,8 +692,7 @@ class Form extends WidgetBase )); } - $widget = new $widgetClass($this->controller, $this->model, $field, $widgetConfig); - $widget->previewMode = $this->previewMode; + $widget = new $widgetClass($this->controller, $field, $widgetConfig); return $this->formWidgets[$field->fieldName] = $widget; } diff --git a/modules/system/traits/ConfigMaker.php b/modules/system/traits/ConfigMaker.php index 6167cc265..3cf5bf6f0 100644 --- a/modules/system/traits/ConfigMaker.php +++ b/modules/system/traits/ConfigMaker.php @@ -28,6 +28,10 @@ trait ConfigMaker */ public function makeConfig($configFile = [], $requiredConfig = []) { + if (!$configFile) { + $configFile = []; + } + /* * Config already made */