A
A
agent11562017-01-21 15:50:39
Yii
agent1156, 2017-01-21 15:50:39

How to style a widget in yii 2?

I use widget for multilingual

<?php
/*
author :: Pitt Phunsanit
website :: http://plusmagi.com
change language by get language=EN, language=TH,...
or select on this widget
*/

namespace common\components;

use Yii;
use yii\base\Component;
use yii\base\Widget;
use yii\bootstrap\ButtonDropdown;
use yii\helpers\Url;
use yii\web\Cookie;

class languageSwitcher extends Widget
{

    public $languages = [
        'ru-RU' => 'РУССКИЙ',
        'en-US' => 'АНГЛИЙСКИЙ',
        'ir-IR' => 'ОСЕТИНСКИЙ',
    ];

    public function init()
    {
        if(php_sapi_name() === 'cli')
        {
            return true;
        }

        parent::init();

        $cookies = Yii::$app->response->cookies;
        $languageNew = Yii::$app->request->get('language');
        if($languageNew)
        {
            if(isset($this->languages[$languageNew]))
            {
                Yii::$app->language = $languageNew;
                $cookies->add(new \yii\web\Cookie([
                    'name' => 'language',
                    'value' => $languageNew
                ]));
            }
        }
        elseif($cookies->has('language'))
        {
            Yii::$app->language = $cookies->getValue('language');
        }

    }

    public function run(){
        $languages = $this->languages;
        $current = $languages[Yii::$app->language];
        unset($languages[Yii::$app->language]);

        $items = [];
        foreach($languages as $code => $language)
        {
            $temp = [];
            $temp['label'] = $language;
            $temp['url'] = Url::current(['language' => $code]);
            array_push($items, $temp);
        }

        echo ButtonDropdown::widget([
            'label' => $current,
            'dropdown' => [
                'items' => $items,
            ],

        ]);
    }

}

This is how the conclusion
<?php echo \common\components\languageSwitcher::widget()?>

How to apply these classes to a widget?
<div aria-labelledby="languages" class="dropdown-menu languages">
                            <ul class="top-arrow">
                                <li><a href="#">ENGLISH</a></li>
                                <li><a href="#">ALLON</a></li>
                                <li><a href="#">РУССКИЙ</a></li>
                            </ul>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-01-21
@slo_nik

Good afternoon.
Used by yii\bootstrap\ButtonDropdown .
Judging by the documentation, there is an options property .
Here is what you use.

echo ButtonDropdown::widget([
            'label' => $current,
            'dropdown' => [
                'items' => $items,
            ],
            'options' => [
                 'class' => 'class_name'
            ]
        ]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question