0

In the , the jui assets are found in paths like: web\assets\135efca3\ in which themes and ui folders are found -Indeed, I don't know why Yii2 makes a varied path segment or fake path-. I think that getAssetPath() method of assetManager should return that path, but I don't know how!

I tried the following debugging code in the actions() method of the controller:

public function actions()
{
   echo Yii::$app->assetManager->getAssetPath(Yii::$app->assetManager->bundles = [
          'yii\jui\JuiAsset'], 'themes');
          die(); //for debugging
} 

However, it just prints /themes.

In other words, I can ask, how could I able to supply the first parameter (object) of getAssetPath()? because, I think it is the problem here.

Edit

I have created the following helper -according to arogachev answer about the path- to get a list of themes.

<?php
namespace common\libs;
use yii;
use yii\web\Controller;

    class JuithemeHelpers
    {
      public static function getThemesList()
      {
        $themesPath =  dirname(Yii::$app->basePath).DIRECTORY_SEPARATOR."vendor".DIRECTORY_SEPARATOR."bower".DIRECTORY_SEPARATOR."jquery-ui".DIRECTORY_SEPARATOR."themes";
        $output = [];
        foreach (scandir($themesPath) as $item){
          if (is_dir($themesPath.DIRECTORY_SEPARATOR.$item) && ($item != '.' && $item !='..')) $output[] = $item;
        }
        return $output;
      }
    }

Then in the view I have made the following:

...
<?= $form->field($model, 'birthdate')->widget(DatePicker::className(), ['clientOptions' => ['dateFormat' => 'yy-mm-dd', 'changeYear' => true, 'yearRange' => sprintf('%s:%s', date('Y')-100,date('Y')-16)],]) ?>
    
      <select onchange="changeTheme(this.value)">
      <?php foreach (JuithemeHelpers::getThemesList() as $item): ?>
              <option value="<?= $item ?>"><?= $item ?></option>
        <?php       endforeach; ?>
      </select>
    
    
    <?= $form->field($model, 'gender_id')->dropDownList($model->getGenderList(), ['prompt' => 'Please Select one...']) ?>


  ...
......
<script>
    function changeTheme(n){  
      s = document.getElementsByTagName('link');
      o = ''
      re = /\/themes\/(.*)\/jquery-ui.css/gi;
      for (i = 0; i < s.length; i++){
        if (s[i].href.match(re)){
          
                o = s[i].href.replace(re.exec(s[i].href)[1],n);
                s[i].href=o;
        }       
      }
    }  
      </script>

I think that now is the time to learn how to package all of this in a widget.

Community
  • 1
  • 1
SaidbakR
  • 13,303
  • 20
  • 101
  • 195
  • 1
    What's the ultimate goal of that? Do you want to change the default used theme? – arogachev Jan 23 '15 at 15:59
  • The goal is to list all the available themes in `themes` folder. This is not available without accessing the `themes` folder and list all of its folders using file system function @arogachev – SaidbakR Jan 23 '15 at 17:12
  • But why you are trying to do that with assets generated folders and not source path folders / files? – arogachev Jan 23 '15 at 17:33
  • Where is the source that I can find themes there? @arogachev – SaidbakR Jan 23 '15 at 17:42

1 Answers1

1

jQuery UI (that comes with framework by default) themes folders are located in /vendor/bower/jquery-ui/themes folder.

You can see it by inspecting $sourcePath ans $css properties of yii\jui\JuiAsset.

Instead of writing the full path you can use alias @bower:

Yii::getAlias('@bower/jquery-ui/themes');

For the listing you can use for example this method:

$themesPath = Yii::getAlias('@bower/jquery-ui/themes');
$results = scandir($themesPath);

foreach ($results as $result) {
    if ($result === '.' || $result === '..' || !is_dir($themesPath . '/' . $result)) {
        continue;
    }

    echo $result . "<br/>";
}
Community
  • 1
  • 1
arogachev
  • 33,150
  • 7
  • 114
  • 117
  • Very good, but there is a little problem here. I want to change the path of the theme in [tag:client-side] i.e using javascript, so we have to use the fake path of the theme. However, Using the path you regarded, I can get a list of themes, and then using javascript and regex I will change the `href` attribute of the css link. – SaidbakR Jan 23 '15 at 21:41