Subscribe: RSS
  • I thought I would share how I fixed file paths in static resources when using media sources. I created a plugin, bound it to OnBeforeDocFormSave and wrote the following:
    <?php
    if($resource->get('class_key') == 'modStaticResource'){
        $resource->set('template', 0); /*optional. i didn't need these to have templates and i assume loading one is slower than not.
                                         plus if a text or html static resource has a template the file will be inserted into the content area like a page*/
        
        $path = $resource->get('content');
        if(count(explode('/', $path)) > 2) return;
        
        $msid = $modx->getOption('default_media_source', null, 'default');
        $ms = $modx->getObject('modMediaSource', $msid);
        $props = $ms->getProperties();
        $path = $props['baseUrl']['value'].explode('assets/usermedia', $path)[1];
        
        $resource->set('content', $path);
    }
    
    return;

    This was written to fit my particular situation but should be easy to modify to fit any other particular situation. It requires that a default media source be set in the context, user group or user settings. In my case all file names were being prepended with assets/usermedia, e.g. assets/usermediaSomePDF.pdf, so I isolated the file name by exploding on that string and then appended it to the baseUrl value from the media source. The correct paths do begin with assets/usermedia but include an additional subdirectory so before doing anything I check the number of forward slashes in the path and if there's more than one I assume it's already correct and return without doing anything.

    If I understood where it was getting the "assets/usermedia" string it might be possible to make this generic but my investigation has thus far not turned that information up.

    Hope this is helpful to somebody.

    Dave