I was attempting to create a site that takes separate subdomains and sends them into a matching context using XRouting. Then have users attached to a context where they can log in and only edit things inside their context. The site has just a few templates and TVs to use site wide.
That seems doable until I tried setting up a custom media source.
I’d like the image TVs to be context aware so that they point to a sub folder that matches the subdomain. For example user1.server.com would have image TVs that only see files inside “assets/accounts/user1/”.
So, I created a Media Source and defined the basePath and baseURL as assets/accounts/[[++context_key]]/ and a user group setting to tie it to the context_key and media source. That seems to work inside the manager. Users can actually open the image TV inputs and only see inside their matching subfolders.
However on the front end the web site doesn’t load the images properly for any secondary context. Instead of creating proper URLs like “/assets/accounts/user1/image.jpg” it is just providing “image.jpg”.
The only context it seems to work on is the default web context. If I create a test page and save have an image in “assets/accounts/web/image.jpg” it will load fine.
Is there some way I can get this to work across all contexts?
So where exactly do you define the setting context_key? In the user group?
I guess this won’t work on the front-end, when you aren’t logged in as this exact user.
Maybe try using a context-setting instead, or if this doesn’t work, use a custom snippet (e.g. [[getMyMediaPath]]) instead of assets/accounts/[[++context_key]]/ in the basePath/baseURL of the media source. Then you have more flexibility in the snippet code, to return the correct path based on the request/status.
context_key is the key for the context defined in the context settings. If you work with different contexts, you will have different context keys. The Manager has the context key “mgr,” and the default website has “web.” You can define additional keys yourself when you create additional contexts.
I use a snippet called `get_mediasource_context_key`, which attempts to determine the correct context key in every situation. It does this either based on the user settings (in the Manager, for editors who are only supposed to access a single context, via the `contextMediasourcePath` option) or based on the context (frontend). As a fallback in the Manager, the resource ID is determined using the Manager URL referrer:
$path = '';
if ($modx->context->key=='mgr') {
$path = $modx->getOption('contextMediasourcePath', $scriptProperties, null); // from user settings
if (empty($path)) {
preg_match('/id=(\d+)/', $_SERVER['HTTP_REFERER'], $matches);
$id = $matches[1] ?? null;
if (!empty($id)) $resource = $modx->getObject('modResource', $id);
if (isset($resource)) $path = $resource->get('context_key');
}
} else {
$path = $modx->resource->get('context_key');
}
return $path;
Created a Media Source and defined the basePath and baseURL as assets/content/[[!get_mediasource_context_key]]/
How well does that [[!get_mediasource_context_key]] snippet work for a new document that hasn’t been saved yet? Google Gemini suggested it might fail. And it also might not work from the dedicated media browser if no document is open.
When I test the [[!get_mediasource_context_key]] snippet I get the same results as the [[++context_key]] placeholder.
Users in the manager are properly seeing the image picker restricted to their subfolder as expected, but when viewing the front end website user1.server.com fails to resolve the media source and just returns image.jpg instead of /assets/account/user1/image.jpg.
The default web context still works though as it did with [[++context_key]]. Accessing pages in that context returns assets/accounts/web/image.jpg.
EDIT:
I figured out why the snippet wasn’t working. Apparently I didn’t have the TV > context mapping set to use the right media source. Doing some testing to make sure it isn’t breaking somewhere not immediately obvioius.
Yes, you have to save the resource first. Otherwise, MODX won’t know which context it belongs to. However, as a restricted user, the user settings would take effect anyway.