'form-control', ]; protected $containerOptions = [ 'class' => 'form-group', ]; protected $labelOptions = [ 'class' => 'control-label', ]; protected $errorOptions = [ 'class' => 'help-block error', ]; protected $attribute; protected $model; public static function renderActiveSingle(ActiveRecord $model, $attribute, $params = []) { $instance = static::instantiate($model, $attribute, $params); return $instance->renderSingle(); } protected static function instantiate(ActiveRecord $model, $attribute, $params = []) { $instance = new static; $instance->setModel($model); $instance->setAttribute($attribute); foreach ($params as $param => $value) { $instance->set($param, $value); } $instance->init(); return $instance; } protected function setModel(ActiveRecord $model) { $this->model = $model; } protected function setAttribute($attribute) { $this->attribute = $attribute; } protected function set($key, $value) { if (is_array($this->$key) && is_array($value)) { $this->$key = \CMap::mergeArray($this->$key, $value); } } public function init() { Select2Asset::register(); $this->defaultOptions = \CMap::mergeArray($this->defaultOptions, [ 'language' => Yii::app()->language, ]); $this->options = \CMap::mergeArray($this->defaultOptions, $this->options); } protected function renderSingle() { \CHtml::resolveNameID($this->model, $this->attribute, $this->htmlOptions); $group = \CHtml::activeDropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions); $id = $this->htmlOptions['id']; $options = \CJavaScript::encode($this->options); $script = <<clientScript->registerScript('select2-' . (\CHtml::$count++), $script); return $group; } public static function renderActive(ActiveRecord $model, $attribute, $params = []) { $instance = static::instantiate($model, $attribute, $params); return $instance->renderActiveField(); } protected function renderActiveField() { \CHtml::resolveNameID($this->model, $this->attribute, $this->htmlOptions); $group = \CHtml::openTag('div', $this->containerOptions); $group .= \CHtml::activeLabelEx($this->model, $this->attribute, $this->labelOptions); $group .= \CHtml::activeDropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions); $group .= \CHtml::error($this->model, $this->attribute); $group .= \CHtml::closeTag('div'); $id = $this->htmlOptions['id']; $options = \CJavaScript::encode($this->options); $script = <<clientScript->registerScript('select2-' . (\CHtml::$count++), $script); return $group; } }