/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
common/widgets/Select2Widget.php
84 строки
2 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace common\widgets; use common\assets\Select2Asset; use yii\helpers\ArrayHelper; use yii\helpers\Html; use yii\widgets\InputWidget; /** * Виджет - Select2 * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2020 */ class Select2Widget extends InputWidget { /** * Значение по умолчанию. */ public $selected = null; /** * Родительские элементы * @var null */ protected $parentSelect = null; /** * @inheritdoc */ public function init() { parent::init(); if ($this->hasModel()) { $this->name = empty($this->options['name']) ? Html::getInputName($this->model, $this->attribute) : $this->options['name']; $this->value = Html::getAttributeValue($this->model, $this->attribute); } if (isset($this->options['parent_id'])) { if (is_array($this->options['parent_id'])) { $this->parentSelect = implode(',', $this->options['parent_id']); } else { $this->parentSelect = $this->options['parent_id']; } } if ($id_name = ArrayHelper::getValue($this->options, 'id_name')) { $this->options['id'] = $id_name; } } /** * @inheritdoc */ public function run() { echo $this->render('select2.twig', [ 'id' => $this->options['id'], 'url' => ArrayHelper::getValue($this->options, 'source'), 'name' => $this->name, 'options' => $this->options, 'class' => ArrayHelper::getValue($this->options, 'class'), 'data_name' => ArrayHelper::getValue($this->options, 'data_name'), 'width' => ArrayHelper::getValue($this->options, 'width'), 'data' => ArrayHelper::getValue($this->options, 'data'), 'selected' => $this->selected, 'value' => ArrayHelper::getValue($this->selected, '0.id'), 'required' => $this->model->isAttributeRequired($this->attribute), 'parent' => $this->parentSelect ]); $this->registerPlugin(); } /** * @inheritdoc */ protected function registerPlugin() { $view = $this->getView(); Select2Asset::register($view); } }