Open Media Browser in specific folder October CSM builder plugin

241 Views Asked by At

Hi I'm looking for some idea how to do it

In my own modules ( articles / gallery / carousel ... ) created in October CMS Builder plugin using filed type mediafinder for images and documents.

Im need in my plugins open backend media browser - directly in user defined folders?

for example

articles in folder: /storage/app/media/articles

gallery in folder: /storage/app/media/gallery, or better /storage/app/media/gallery/id (if id gallery exists - or create subfolder if not exists )

carousel in folder: /storage/app/media/carousel

I don't need any restriction - user can change folders directly from media browser if wants. But opened must by in subfolder defined in plugin.

thank you

Vaclav

2

There are 2 best solutions below

0
On

There is currently no way to set the default media finder path, this feature was requested in GitHub but never implemented. Issue link here.

EDIT: Following up on Hardik's comment, that is actually a really nice workaround for adding a path to media manager/finder.

You can modify the controllers create/update methods to include the path in the session. I'll include an example for the update method since the link he provided has the create one.

You can also use the MediaLibrary API to handle checking for a folder and creating a new one if needed for your gallery. Specifically the folderExists(string $path) and makeFolder(string $path) methods

class Articles extends Controller
{
    use \Backend\Traits\SessionMaker;

    ...
    public function getId($suffix = NULL) {
        return 'MediaFinder-formMedia-media';
    }

    public function update($recordId, $context = null) {
        // Add path on the second argument
        $this->putSession('media_folder', '/articles');
        return $this->asExtension('FormController')->update($recordId, $context);
    }

    ...
}
0
On

You can simply add a small code snippet to your controller to control which location to open in the media manager widget

Ref: https://tutorialmeta.com/october-cms/set-custom-path-october-cms-media-manager-widget

class Movies extends Controller
{
    use \Backend\Traits\SessionMaker;
    // ... other code

    // this is to write session to media widget session
    public function getId($suffix = NULL) {
        return 'MediaManager-ocmediamanager';
    }

    public function create() {
        $this->putSession('media_folder', '/session-check/inner-level/');
        // put your path here -------------- ^
        return $this->asExtension('FormController')->create();
    }
}

this snippet will let you open the media manager widget with location session-check/inner-level/ when you are creating a record.

if you want the same with edit, just add update action function with same code as create function

please comment if you have any doubt.