Skip to content

Dynamic roots

Alex Yashkin edited this page Jun 13, 2017 · 1 revision

Inspired by issue #2. Setting root path dynamically in widget.

  1. Create custom action instead ConnectorAction:
public function actionConnector()
{
    $path = \Yii::$app->request->getQueryParam('path');
    $path = trim($path, '/');
    // todo: clean and validate path
    if (empty($path)) {
        $path = 'uploads';
    }
    $options = [
        'roots' => [
            [
                'driver' => 'LocalFileSystem',
                'path' => Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . $path,
                'URL' => Yii::getAlias('@web') . '/' . $path . '/',
                'mimeDetect' => 'internal',
                'imgLib' => 'gd',
                'accessControl' => function ($attr, $path) {
                    return (strpos(basename($path), '.') === 0) ? !($attr == 'read' || $attr == 'write') : null;
                },
            ],
        ],
    ];
    \Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
    (new \elFinderConnector(new \elFinder($options)))->run();
}
  1. Extend CKEditorAction:
class CustomCKEditorAction extends \alexantr\elfinder\CKEditorAction
{
    public function run()
    {
        $path = \Yii::$app->request->getQueryParam('path');
        $this->connectorRoute = [$this->connectorRoute, 'path' => $path];
        return parent::run();
    }
}
  1. Set news class in CKEditor config:
    'ckeditor' => [
        'class' => CustomCKEditorAction::className(),
        'connectorRoute' => 'connector',
    ],
  1. Set path in CKEditor widget:
'filebrowserBrowseUrl' => yii\helpers\Url::to(['elfinder/ckeditor', 'path' => 'uploads/pictures/cats']),
Clone this wiki locally