Hi, I already did something like this and described on January 22 (will see above). But this is not an effective way because for each group you need to create a different TV (e.g.: photo_sport, photo_culture, photo_nature).
Here we consider only the simple example of 3 groups and one tv photo. Ultimately there will be more groups and many more different TVs, so it is important not to make separate TVs for each group in a given template.
OK, back to halftrainedharry’s idea. Maybe you could use the pagetitle of the parent folder as part of your media source path, or have one TV with the folder name that’s value is inherited down the tree and used as part of the media source path.
No, a particular user will be able to belong to one group. For example, a user (editor) from the sports section will belong only to this section, he will not be able to write articles, for example: nature. The exception is the administrator, who will have access to everything.
The administrator could be a problem, but that’s not an issue if the admin will have a separate media source that’s above all the others.
What I’m thinking of is a snippet that checks to see what group the user belongs to and returns the name of that group. That part is easy. Then the TV could have a snippet tag for that snippet so that part of the media source path is the name of the group. I have no experience with dynamic media sources, so I don’t know if it would work, and there could be cache issues that would make it fail, but it’s all I can come up with.
I had another thought about my previous idea. If I’m understanding your chart, you have a different parent resource for every group. If there is a text TV connected to its template that contains the media source path for its group, all the child resources can inherit the value of the TV. You’d only need one TV. In theory the Media Source TV’s fields could contain a tag for that TV. So you’d only have two TVs total. TBH, I’m not sure this makes sense, but I thought I’d mention it.
You have to write a custom snippet that defines the path, depending on the request. The most important part is to detemine the current resource-ID.
MIGX has the snippet “migxResourceMediaPath” (which serves a similar purpose) integrated, that works out the resource-ID like this:
This code can be copied and used in a custom snippet.
Here is some example code (that may or may not work):
Media Source
Set the values of the settings “basePath” and “baseUrl” to a MODX snippet tag for your custom snippet → [[DynamicMediaSourcePath]]
Snippet “DynamicMediaSourcePath”
<?php
// Determine resource-ID
$docid = null;
// on frontend
if (is_object($modx->resource)) {
$docid = $modx->resource->get('id');
}
// on manager resource/update page
else {
// We do this to read the &id param from an Ajax request
$parsedUrl = parse_url($_SERVER['HTTP_REFERER']);
parse_str($parsedUrl['query'], $parsedQuery);
$action = $parsedQuery['a'] ?? '';
if ($action == 'resource/update'){
$docid = (int)$parsedQuery['amp;id'] ?? (int)$parsedQuery['id'] ?? 0;
}
}
if (empty($docid)) {
$modx->log(MODX_LOG_LEVEL_DEBUG, '[DynamicMediaSourcePath]: docid could not be determined.');
return ""; // Or maybe return a fallback path
}
// Load the resource
$resource = $modx->getObject('modResource', $docid);
if (!$resource){
$modx->log(MODX_LOG_LEVEL_DEBUG, '[DynamicMediaSourcePath]: Resource with ID=' . $docid . ' not found.');
return ""; // Or maybe return a fallback path
}
// Return the path according to the value of the "parent" field.
switch ($resource->get("parent")) {
case 1:
return "assets/images01/";
break;
case 2:
return "assets/images02/";
break;
default:
return ""; // Or maybe return a fallback path
}
Adjust the code in the switch statement to your needs.
If you need the parent-ID that is more than 1 level up, you could use the MODX function getParentIds.
It worked ! @halftrainedharry thank you and @bobray, for taking your time and helping to solve the problem. The dynamic path in media sources works great. It took a while because my actions were stretched out over time. Once again, the MODX community did a great job.