Closed dynasource closed 4 years ago
Widget::widget()
uses output buffering so your ex might break consistency.
How about this, if $class
is a config array then pick out the class name.
public function widget($class, $config = [])
{
/* @var $class \yii\base\Widget */
$config['model'] = $this->model;
$config['attribute'] = $this->attribute;
$config['view'] = $this->form->getView();
if (is_array($class) && isset($class['class'])) {
$config = array_merge($class, $config);
$class = $class['class'];
}
$this->parts['{input}'] = $class::widget($config);
return $this;
}
isnt Widget::widget() the same as Yii::createObject($config)->run()?
It handles output buffering, https://github.com/yiisoft/yii2/blob/master/framework/base/Widget.php#L94
ahh, I see, for scenario's in which echo's are done inside init. This should be prohibited in the first place ;) https://github.com/yiisoft/yii2/issues/11915
Your solution is better then!
@dynasource so you want to make the following code work, right?
Widget::widget([
'class' => BarWidget::class,
'foo' => 'baz'
]);
correct
As far as I see, the only reason is to make it consistent with Yii::createObject()
, right?
definately. After making many, many form widgets, this one is always surprising because of it being inconsistent with other object & array mappings like with Yii::createObject()
What steps will reproduce the problem?
Before using this function, you have to separate the class from its options. In 99% of the time one should not have to do this.
What is the expected result?
I would expect similar functionality like with Yii::createObject. Passing around arrays with classes should be supported
What do you get instead?
You have to separate it everytime yourself. The current implementation is not flexible and inconsistent with other area's where configuration arrays with included class parameters are supported.
Instead of:
I would suggest the following (it is BC):