public static function getAttributeValue($model, $attribute)
{
if (!preg_match('/(^|.*\\])([\\w\\.]+)(\\[.*|$)/', $attribute, $matches)) {
throw new InvalidParamException('Attribute name must contain word characters only.');
}
$attribute = $matches[2];
$value = $model->{$attribute};
if ($matches[3] !== '') {
foreach (explode('][', trim($matches[3], '[]')) as $id) {
if ((is_array($value) || $value instanceof \ArrayAccess) && isset($value[$id])) {
$value = $value[$id];
} else {
return null;
}
}
}
// https://github.com/yiisoft/yii2/issues/1457
if (is_array($value)) {
foreach ($value as $i => $v) {
if ($v instanceof ActiveRecordInterface) {
$v = $v->getPrimaryKey(false);
$value[$i] = is_array($v) ? json_encode($v) : $v;
}
}
} elseif ($value instanceof ActiveRecordInterface) {
$value = $value->getPrimaryKey(false);
return is_array($value) ? json_encode($value) : $value;
}
return $value;
}
public function run() { $options = $this->options; if (isset($options['id'])) { $id = $options['id']; } else { $id = 'editor'; } if (isset($options['name'])) { $name = $options['name']; } elseif ($this->hasModel()) { $name = BaseHtml::getInputName($this->model, $this->attribute); } else { $name = $this->name; } if (isset($options['value'])) { $value = $options['value']; } elseif ($this->hasModel()) { $value = BaseHtml::getAttributeValue($this->model, $this->attribute); } else { $value = $this->value; } echo Html::beginTag('script', ['id' => $id, 'type' => 'text/plain', 'name' => $name, 'style' => "height:{$this->height}"]); echo $value; echo Html::endTag('script'); if (!isset($options['config'])) { $options['config'] = []; } $ueditorConfig = ArrayHelper::merge(['serverUrl' => Url::to(['ueditor/controller'])], $options['config']); $config = Json::encode($ueditorConfig); $view = $this->getView(); UEditorAsset::register($view); $view->registerJs("var ue = UE.getEditor('{$id}', {$config});"); }