/** * @inheritdoc * @throw NotSupportedException */ public function run() { if ($this->hasModel()) { if (!is_null($this->value)) { if (!in_array($this->attribute, $this->model->attributes())) { throw new NotSupportedException('Unable to set value of the property \'' . $this->attribute . '\'.'); } $stash = $this->model->{$this->attribute}; $this->model->{$this->attribute} = $this->value; } $output = Html::activeListBox($this->model, $this->attribute, $this->items, $this->options); if (isset($stash)) { $this->model->{$this->attribute} = $stash; } } else { $output = Html::listBox($this->name, $this->value, $this->items, $this->options); } $js = 'jQuery(\'#' . $this->options['id'] . '\').select2(' . Json::htmlEncode($this->clientOptions) . ');'; if (Yii::$app->getRequest()->getIsAjax()) { $output .= Html::script($js); } else { $view = $this->getView(); Select2Asset::register($view); Select2LanguageAsset::register($view); $view->registerJs($js); } return $output; }
/** * @param string $language * @dataProvider languageDataProvider */ public function testBundleHasFile2($language) { $assetManager = Yii::$app->getAssetManager(); $assetManager->bundles[Select2LanguageAsset::className()] = ['language' => $language]; $bundle = $assetManager->getBundle(Select2LanguageAsset::className()); $this->assertInstanceOf('yii\\jquery\\select2\\Select2LanguageAsset', $bundle); $this->assertArrayHasKey(0, $bundle->depends); $this->assertEquals('yii\\jquery\\select2\\Select2Asset', $bundle->depends[0]); $this->assertArrayHasKey(0, $bundle->js); $this->assertFileExists($bundle->basePath . DIRECTORY_SEPARATOR . $bundle->js[0]); }