Added new makeFormWidget() method to WidgetMaker for rendering form widgets individually

This commit is contained in:
Samuel Georges 2015-08-07 19:06:04 +10:00
parent ad60afebc4
commit fc04bd1b4c
5 changed files with 52 additions and 11 deletions

View File

@ -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) * **Build 287** (2015-08-03)
- Introduced new **MarkdownEditor** form widget (see Backend > Forms docs). - Introduced new **MarkdownEditor** form widget (see Backend > Forms docs).

View File

@ -104,12 +104,12 @@ class Repeater extends FormWidgetBase
if (!is_array($itemIndexes)) return; if (!is_array($itemIndexes)) return;
foreach ($itemIndexes as $itemIndex) { foreach ($itemIndexes as $itemIndex) {
$this->makeFormWidget($itemIndex); $this->makeItemFormWidget($itemIndex);
$this->indexCount = max((int) $itemIndex, $this->indexCount); $this->indexCount = max((int) $itemIndex, $this->indexCount);
} }
} }
protected function makeFormWidget($index = 0) protected function makeItemFormWidget($index = 0)
{ {
$loadValue = $this->getLoadValue(); $loadValue = $this->getLoadValue();
if (!is_array($loadValue)) $loadValue = []; if (!is_array($loadValue)) $loadValue = [];
@ -131,7 +131,7 @@ class Repeater extends FormWidgetBase
$this->indexCount++; $this->indexCount++;
$this->prepareVars(); $this->prepareVars();
$this->vars['widget'] = $this->makeFormWidget($this->indexCount); $this->vars['widget'] = $this->makeItemFormWidget($this->indexCount);
$this->vars['indexValue'] = $this->indexCount; $this->vars['indexValue'] = $this->indexCount;
$itemContainer = '@#'.$this->getId('items'); $itemContainer = '@#'.$this->getId('items');

View File

@ -1,6 +1,7 @@
<?php namespace Backend\Traits; <?php namespace Backend\Traits;
use Lang; use Lang;
use Backend\Classes\FormField;
use Backend\Classes\WidgetManager; use Backend\Classes\WidgetManager;
use SystemException; use SystemException;
@ -19,10 +20,10 @@ trait WidgetMaker
/** /**
* Makes a widget object with the supplied configuration file. * Makes a widget object with the supplied configuration file.
* @param string $class Widget class name * @param string $class Widget class name
* @param array $configuration An array of config. * @param array $widgetConfig An array of config.
* @return WidgetBase The widget object * @return WidgetBase The widget object
*/ */
public function makeWidget($class, $configuration = []) public function makeWidget($class, $widgetConfig = [])
{ {
$controller = property_exists($this, 'controller') && $this->controller $controller = property_exists($this, 'controller') && $this->controller
? $this->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);
} }
} }

View File

@ -460,7 +460,7 @@ class Form extends WidgetBase
continue; continue;
} }
$widget = $this->makeFormWidget($field); $widget = $this->makeFormFieldWidget($field);
$widget->bindToController(); $widget->bindToController();
} }
@ -674,7 +674,7 @@ class Form extends WidgetBase
/** /**
* Makes a widget object from a form field object. * Makes a widget object from a form field object.
*/ */
protected function makeFormWidget($field) protected function makeFormFieldWidget($field)
{ {
if ($field->type != 'widget') { if ($field->type != 'widget') {
return null; 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; return $this->formWidgets[$field->fieldName] = $widget;
} }
@ -820,7 +820,7 @@ class Form extends WidgetBase
} }
if ($field->type == 'widget') { if ($field->type == 'widget') {
$widget = $this->makeFormWidget($field); $widget = $this->makeFormFieldWidget($field);
return $widget->showLabels; return $widget->showLabels;
} }

View File

@ -1,5 +1,5 @@
<!-- Widget --> <!-- Widget -->
<?php <?php
$widget = $this->makeFormWidget($field); $widget = $this->makeFormFieldWidget($field);
?> ?>
<?= $widget->render() ?> <?= $widget->render() ?>