We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Anyone have time to take a look at this? I still can't get it to work and now even the standard snippet MIGX comes with doesn't work.
      Ben Morrison
      Hacking templates to pieces since 2003
    • Hm. So it would be quite possible to make template (or parent) and user based media sources. I love it!

      I'll play with this and see what it'll do.

      Which version of MODX are you working with?
        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
      • There is a typo in the snippet heading comments:
         * &createfolder (optional) boolean whether or not to create

        That should be &createFolder, which is what the getOption function is looking for:
        $createfolder = $modx->getOption('createFolder', $scriptProperties, false);
          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
        • Works great now.
          [[migxResourceMediaPath? &pathTpl=`assets/images/{templatename}/{id}/` &createFolder=`1`]]

          produces an image URL like this:
          http://localhost/revo231/assets/images/BaseTemplate/1/beach7_small.jpg
            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
          • Using {alias} instead of {id} on the home page
            http://localhost/revo231/assets/images/BaseTemplate/index/beach10_small.jpg

            and this for Page Two:
            http://localhost/revo231/assets/images/BaseTemplate/page-two/yafo-beach-me-800.jpg
              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
            • Be warned... a space in the template name will kill your page. It will create the directories (at least on OS X and probably on Windows), but something will choke on it causing your front-end to show a white page. Couldn't find any errors anywhere, just no page.

              This modification of line 97 of the snippet seems to work:
                          $path = str_replace('{templatename}', str_replace(' ', '_', $template->get('templatename')), $path);
              

              http://localhost/revo231/assets/images/Base_Template/index/beach7_small.jpg
                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
                • 23018
                • 353 Posts
                Using the "name" of a template, resources, etc will most likely end bad. Names are changing all the time. I'd recommend using the id and adding a file to the directory as a visual fallback.

                assets/templates/200/standard.txt

                If you want to, you can add an image to this directory displaying the name or id:

                Here is an example for docid...

                    if(!file_exists($fullpath . $docid . '.gif')){
                        $placeholder = file_get_contents("http://placehold.it/150x150&text=$docid");
                        $image = $fullpath . $docid . '.gif';
                        
                        if(!@file_put_contents($image, $placeholder)){
                            $msg[] = $image . 'could not be created';
                        }
                        else {
                            $msg[] = $image . 'created';
                        }
                        
                        $msg[] = $docid.'gif created at' . $fullpath;
                    }
                    $modx->log(MODX_LOG_LEVEL_ERROR, print_r($msg,true));
                


                The result looks like this:



                Cheers,

                pepebe
                  Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
                • Well, hopefully the New and Improved file browser that will be done someday will show the path.
                    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
                  • Ok. All that said, and editing the snippet to get rid of all of those named placeholders, here's what I did add to allow the use of username.
                    if ($user = $modx->user) {
                        $path = str_replace('{username}', $modx->user->get('username'), $path);
                    }


                    http://localhost/revo231/assets/images/admin/beach7_small.jpg
                      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
                      • 50312
                      • 34 Posts
                      Quote from: sottwell at Feb 01, 2014, 04:51 AM
                      You can use MODX tags in the media source path and URL, so I would think something like
                      /assets/images/[[*template]]/

                      would do the trick.

                      Is this true anymore? Working on Revo 2.4.2-pl and this doesn't seem to work.
                      I would like to have my media source's basepath + baseurl depend on the alias - the directories are already created, I just want to access them via this dynamic media source.

                      But this:

                      Basepath: assets/images/[[*alias]]/
                      Base URL: assets/images/[[*alias]]/

                      leads me to assets/images/ in the media browser. Same goes for assets/images/[[*template]]/

                      Any help would be appreciated.