From fc04bd1b4c958365bad18567d55024968786a9b3 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Fri, 7 Aug 2015 19:06:04 +1000 Subject: [PATCH] Added new makeFormWidget() method to WidgetMaker for rendering form widgets individually --- CHANGELOG.md | 3 ++ modules/backend/formwidgets/Repeater.php | 6 +-- modules/backend/traits/WidgetMaker.php | 44 +++++++++++++++++-- modules/backend/widgets/Form.php | 8 ++-- .../widgets/form/partials/_field_widget.htm | 2 +- 5 files changed, 52 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fbbcf45ec..a79208232 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +* **Build 28x** (2015-08-xx) + - Added new `makeFormWidget()` method to `WidgetMaker` trait for rendering form widgets individually. This method is now available to backend controllers, controller behaviors and widgets themselves. Check to make sure your classes do not define a conflicting method of this name. + * **Build 287** (2015-08-03) - Introduced new **MarkdownEditor** form widget (see Backend > Forms docs). diff --git a/modules/backend/formwidgets/Repeater.php b/modules/backend/formwidgets/Repeater.php index 23baa6d8a..7520dbd55 100644 --- a/modules/backend/formwidgets/Repeater.php +++ b/modules/backend/formwidgets/Repeater.php @@ -104,12 +104,12 @@ class Repeater extends FormWidgetBase if (!is_array($itemIndexes)) return; foreach ($itemIndexes as $itemIndex) { - $this->makeFormWidget($itemIndex); + $this->makeItemFormWidget($itemIndex); $this->indexCount = max((int) $itemIndex, $this->indexCount); } } - protected function makeFormWidget($index = 0) + protected function makeItemFormWidget($index = 0) { $loadValue = $this->getLoadValue(); if (!is_array($loadValue)) $loadValue = []; @@ -131,7 +131,7 @@ class Repeater extends FormWidgetBase $this->indexCount++; $this->prepareVars(); - $this->vars['widget'] = $this->makeFormWidget($this->indexCount); + $this->vars['widget'] = $this->makeItemFormWidget($this->indexCount); $this->vars['indexValue'] = $this->indexCount; $itemContainer = '@#'.$this->getId('items'); diff --git a/modules/backend/traits/WidgetMaker.php b/modules/backend/traits/WidgetMaker.php index d61bf2d9d..a069f09e6 100644 --- a/modules/backend/traits/WidgetMaker.php +++ b/modules/backend/traits/WidgetMaker.php @@ -1,6 +1,7 @@ controller ? $this->controller @@ -34,6 +35,43 @@ trait WidgetMaker ])); } - return new $class($controller, $configuration); + return new $class($controller, $widgetConfig); + } + + /** + * Makes a form widget object with the supplied form field and widget configuration. + * @param string $class Widget class name + * @param mixed $fieldConfig A field name, an array of config or a FormField object. + * @param array $widgetConfig An array of config. + * @return FormWidgetBase The widget object + */ + public function makeFormWidget($class, $fieldConfig = [], $widgetConfig = []) + { + $controller = property_exists($this, 'controller') && $this->controller + ? $this->controller + : $this; + + if (!class_exists($class)) { + throw new SystemException(Lang::get('backend::lang.widget.not_registered', [ + 'name' => $class + ])); + } + + if (is_string($fieldConfig)) { + $fieldConfig = ['name' => $fieldConfig]; + } + + if (is_array($fieldConfig)) { + $formField = new FormField( + array_get($fieldConfig, 'name'), + array_get($fieldConfig, 'label') + ); + $formField->displayAs('widget', $fieldConfig); + } + else { + $formField = $fieldConfig; + } + + return new $class($controller, $formField, $widgetConfig); } } diff --git a/modules/backend/widgets/Form.php b/modules/backend/widgets/Form.php index 1b2aa8a02..6c1a70eb4 100644 --- a/modules/backend/widgets/Form.php +++ b/modules/backend/widgets/Form.php @@ -460,7 +460,7 @@ class Form extends WidgetBase continue; } - $widget = $this->makeFormWidget($field); + $widget = $this->makeFormFieldWidget($field); $widget->bindToController(); } @@ -674,7 +674,7 @@ class Form extends WidgetBase /** * Makes a widget object from a form field object. */ - protected function makeFormWidget($field) + protected function makeFormFieldWidget($field) { if ($field->type != 'widget') { return null; @@ -700,7 +700,7 @@ class Form extends WidgetBase )); } - $widget = new $widgetClass($this->controller, $field, $widgetConfig); + $widget = $this->makeFormWidget($widgetClass, $field, $widgetConfig); return $this->formWidgets[$field->fieldName] = $widget; } @@ -820,7 +820,7 @@ class Form extends WidgetBase } if ($field->type == 'widget') { - $widget = $this->makeFormWidget($field); + $widget = $this->makeFormFieldWidget($field); return $widget->showLabels; } diff --git a/modules/backend/widgets/form/partials/_field_widget.htm b/modules/backend/widgets/form/partials/_field_widget.htm index 5aed7ac48..1612ce9ed 100644 --- a/modules/backend/widgets/form/partials/_field_widget.htm +++ b/modules/backend/widgets/form/partials/_field_widget.htm @@ -1,5 +1,5 @@ makeFormWidget($field); + $widget = $this->makeFormFieldWidget($field); ?> render() ?> \ No newline at end of file