cozumel424 / yii2-themepicker

This is a simple extension to pick themes. The user chooses a theme from a dropdown box and the theme choice is stored in a cookie, the extension could easily be changed to work with a model too.
1 stars 0 forks source link

Can't modify the path for the theme css file #1

Open larry-tx opened 9 years ago

larry-tx commented 9 years ago

Your Themepicker extension seems to be an excellent resource. However, I am having difficulty set $themePath in a variation of the advanced template. Specifically, my directory structure looks like this:

-- yii2  (base application directory)
    -- _protected
        -- backend (what would normally be the backend in the advanced template)
        -- common
        -- console
        -- environments
        -- frontend
        -- tests
        -- vendor
    -- assets
    ...
    -- themes (where the actual theme directories reside)

I have tried multiple ways to define the $themePath variable so that it works and have gotten there, sort of. I can easily get the dropdown to correctly display all my themes by doing this:

Yii::setAlias('themes', realpath(dirname(__FILE__).'/../../../themes')); (in _protected/common/config/bootstrap.php

$themePath = \Yii::getAlias('themes'); (in ThemePicker.php, although I hate channging the source code for an extension)

However, when I try to switch themes, it falls apart because the css file that is handed off has a bizarre path in it. Specifically, the path is:

<link href="L:\xampp\htdocs\yii2\_protected\frontend/themes/almost-victorian/css/site.css" rel="stylesheet">

The path should be:

<link href="L:\xampp\htdocs\yii2\themes/almost-victorian/files/main_style.css" rel="stylesheet">

I'm also using themes from ThemeFactory.net which has the <THEME_NAME/files/main_style.css way of storing the theme.

I've tried in vain to find out where that actual, bizarre path is coming from and how to reset it. I'm not sure if it's coming from your extension or elsewhere.

Can you give me any help on this?

Actually, I just answered my own question. I modified the Themepicker bootstrap.php file to read:

          'pathMap' => [
                //'@app/views' => '@app/themes/' .$themeName ,
                '@app/views' => \Yii::getAlias('themes') . '/' . $themeName,
                '@app/modules' => '@app/themes/' .$themeName . '/modules',

            ],

            //'baseUrl' => '@app/themes/' .$themeName
            'baseUrl' => '/' . \Yii::getAlias('themes') . '/' . $themeName,

You might want to consider including this in your somewhat scanty instructions. I took me quite a while to figure it all out.

Now I have two additional issues.

First, when attempting to switch themes, I have to choose the same theme from the dropdown TWICE before it will actually change. Selecting it just once does nothing.

Second, there don't seem to be any hooks for styling the widget. I'd like to include in my topnav navbar, but that requires adding some very specific classes and such to the select. Do you have any suggestions?

losttheplot commented 8 years ago

To fix the problem with having to select the theme twice, wrap the cookie reader in a conditional to look for a POST first (in Bootstrap.php):

if (\Yii::$app->request->post('theme_picker') !== null && in_array(\Yii::$app->request->post('theme_picker'),  $themes, true)){
    $themeName = \Yii::$app->request->post('theme_picker');
} else {
    $themeName = \Yii::$app->getRequest()->getCookies()->getValue('theme_picker');
}

Add a similar conditional in ThemePicker.php so that the drop-down theme list will also show the selected theme prior to the cookie being reset by the POST.

Alternatively, as my colleague has pointed out to me, one could simply reload the page after the cookie has been set in ThemePicker.php:

self::setCookie(\Yii::$app->request->post('theme_picker'));
\Yii::$app->response->redirect(\yii\helpers\Url::to(\Yii::$app->request->getUrl()));