We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Can I get a step by step lead on how to link a static resource (via the mediaSource browser) to an document placed outside web root so a user can simply use the Static Resource fileselect field to select desired document for attachment.

    This question has been answered by ojchris. See the first response.

      • 44234
      • 219 Posts
      Change the 'basePath' setting for your media source to '../' to move one level up the folder tree. Depends on how folder structure and permissions are setup on your server.
        Find me on Twitter, GitHub or Google+
      • Static resources do not really work with media sources.

        While you can use a media source with its basePath set to outside of the web root, and the static resource will pick up the path from that basePath, it won't pick up the basePath value itself.

        I had to create a chunk with that basePath and prepend the chunk's tags to the path the static resource gets from its media source.

        You could use a plugin to do this automatically, check if the resource has the class_key of "modStaticResource", and if it does get the basePath for your media source and prepend it to the static resource's field value, but so far just prepending the chunk tags is working OK in my situation.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
        • Thanks for your responses.

          I had to create a chunk with that basePath and prepend the chunk's tags to the path the static resource gets from its media source.


          How is that? You mean you place this "../" in the chunk. How do you prepend it to the media source generated path.

          Can you show me a sample script?
          • In short, no script: in the Static Resource field, select your Media Source and select your file. Then at the beginning of the path this will put in the field, put [[$pathPrefix]], so it looks like this
            [[$pathPrefix]]path/to/your/file.pdf



            Now some thoughts on the subject:

            If you're not comfortable remembering to put the chunk tags in the Static Resource field, you could use a plugin to check if the resource being processed is a modStaticResource, and if it is, fetch the path property from the JSON string in the "properties" field of your Media Source (modx_media_sources) and prepend it to the Static Resource path (its content) before doing anything else with it. I'd have to experiment a bit to figure out the best event to use for this.

            If Static Resources properly worked with Media Sources, it would simply indicate what Media Source was selected, and when the resource is requested, if it's a modStaticResource it would get the path prefix from its Media Source's properties field. That way if the Media Source is edited, the changes will be incorporated in the final full path to the file. That's why it's not so easy; you can't just add the Media Source path to the Static Resource path and save it, because then you can't change the path to the files in the Media Source without editing every Static Resource. I think I'm beginning to see how a plugin could do this, though: add {myFiles} to the beginning of the path of all modStaticResource resources onBeforeDocSave, then on processing the request, if the resource requested is a modStaticResource replace the {myFiles} with the path property of the myFiles Media Source.

            What we really need is another MODx tag, for Static Resource. Something like [[#MyFiles]], so you could use it wherever you wanted (including having MODx automatically add its path property to the Static Resource path) when you want to indicate the Media Source's properties (paths, URLs, etc). So something like [[#myFiles? &value=`path`]] would return the Media Source's path property. Hm... an output modifier, perhaps?
            [ed. note: sottwell last edited this post 10 years, 8 months ago.]
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
            • Thank you again. I'll update this thread asap on my results .
              • So far so good. How is it possible redirecting the memeber-only static resource to the login page when unauthorized persons try to view it directly?
                • My admin log in was interfering. I used another browser to successfully redirect. However, I set the the authorize page to the login page (in settings tab: core->site->unauthorize) but when I try to view the static resource page it redirects to the Homepage (which I have set for error situations) rather than the login page. So I'm really not complete with this.

                  Any idea what might be wrong.
                  • This depends on the configuration of your ACL for the groups. If the group does not have at least "load" permission for the protected resource, it can't even know that it exists in order to check it for permissions, thus getting the error page instead of the unauthorized page.
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                    • I have the Context Access set to Context (load, list, view, save, remove, copy, view_unpublished) and Resource Group Access set to Resource (add_children, create, copy, delete, list, load, move, publish, remove, save, steal_lock, undelete, unpublish, view).

                      All other pages in the group work accordingly except for the Static Resource. I have my ACL setting attached.
                      [ed. note: ojchris last edited this post 10 years, 8 months ago.]