Context based media sources

Short question: Are context based media sources somehow possible for an image field? Would be nice to have this.

If you leave the image field at not overriding the media source, there’s a contentblocks.source system setting you should be able of overriding per context.

I’ve also done it using one of MIGX’s components to make a dynamic, context-based path for a single media source.

@mhamstra Thanks!

@isaacniebeling That leads to another question: Are context based paths i.e. by using placeholders like {assets_url} or [[++assets_url]] possible in MODX media sources?

Yup, sort of – you can use snippets in media source paths. For example, my path looks like assets/sites/[[!cc_media_source_path]]/uploads/, and then my cc_media_source_path snippet looks like:

$params = array(
    'pathTpl' => '{context_key}',
    'createFolder' => 1,
$path = $modx->runSnippet('migxResourceMediaPath', $params);
if(!$path) {
    $user = $modx->getUser();
    $settings = $user->getSettings();
    $path = $settings['media_source_basepath'] ? $settings['media_source_basepath'] : $modx->getOption('media_source_basepath', null, 'web');

return $path;

I could just use the migxResourceMediaPath snippet directly, but I had some other manipulation I wanted to do that I couldn’t get nailed down without wrapping it (in particular, making my stuff be settable on a user-setting level). There are a couple of ways to skin that particular cat, though, and some of them may even be better than this – I’m just not sure. Check into the migxResourceMediaPath docs to find out what placeholders it supports – that may do a good enough job for you.

If you just want that in ContentBlocks, and not the entire media source, the upload paths have placeholders built in too. [[++assets_url]] works there, as do a whole bunch of others including resource fields (e.g. [[+alias]]) and TVs too.

But that’s only used for the upload path and not for the initial opened folder in the file browser?