We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20471
    • 40 Posts
    I'm trying to set up a dynamic Media Source with MIGX as described here:
    http://rtfm.modx.com/display/ADDON/MIGX.Use+resource-specific+media+source+and+multifile-uploader

    I'm only interested in the dynamic part (not the multifile-uploader), so I'm only changing basepath and baseurl to:
    [[migxResourceMediaPath? &pathTpl=`assets/resourceimages/{id}/`]]


    Problem is that I'm also using Babel for multilanguage support, and this particular TV is (and should be) synced between translations. With the solution above I get one folder for each translation, meaning my users have to (in this case) upload a portrait for each translation.

    Is there some way of replacing {id} above to always be the ID of the default language resource? Something like:
    [[migxResourceMediaPath? &pathTpl=`assets/resourceimages/[[BabelTranslation? &contextKey=`web` &resourceId=`{id}`]]/`]]


    Any help appreciated!

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

    [ed. note: mrtnrsl last edited this post 11 years, 1 month ago.]
      • 4172
      • 5,888 Posts
      you would need a custom migxResourceMediaPath - snippet
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 20471
        • 40 Posts
        Thanks Bruno,

        Seems to be pretty straightforward, but something is wrong.
        I've added this to my custom snippet:
        $docid = $modx->runSnippet('BabelTranslation', array('contextKey' => 'web', 'resourceId' => $docid));


        The folder is created, and the snippet returns a correct id (logged it to the error log). However, the MODX browser doesn't like it. When trying to upload a file, the upload never finishes. And the default "No files match blabla" string usually shown in empty folders is gone. And this error strangely occurs even if I only log the code above.

        My full snippet is as follows:
        <?php
        $pathTpl = $modx->getOption('pathTpl', $scriptProperties, '');
        $docid = $modx->getOption('docid', $scriptProperties, '');
        
        if (empty($docid) && $modx->getPlaceholder('docid')) {
            $docid = $modx->getPlaceholder('docid');
        }
        if (empty($docid)) {
        
            if (is_Object($modx->resource)) {
                $docid = $modx->resource->get('id');
            } else {
        
                $parsedUrl = parse_url($_SERVER['HTTP_REFERER']);
                parse_str($parsedUrl['query'], $parsedQuery);
        
                if (isset($parsedQuery['amp;id'])) {
                    $docid = $parsedQuery['amp;id'];
                } elseif (isset($parsedQuery['id'])) {
                    $docid = $parsedQuery['id'];
                }
            }
        }
        // Same Media Source for all languages
        $modx->log(modX::LOG_LEVEL_ERROR, 'Result: '.$modx->runSnippet('BabelTranslation', array('contextKey' => 'web', 'resourceId' => $docid)));
        $docid = $modx->runSnippet('BabelTranslation', array('contextKey' => 'web', 'resourceId' => $docid));
        
        $path = str_replace('{id}', $docid, $pathTpl);
        $fullpath = $modx->getOption('base_path') . $path;
        
        if (!file_exists($fullpath)) {
            mkdir($fullpath, 0755, true);
        }
        
        return $path;


        Any ideas?
          • 4172
          • 5,888 Posts
          what happens, when you change the mediasource-pathes to a hardcoded one(the same as the generated one).
          Does the file get uploaded then?
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 20471
            • 40 Posts
            Yes. And it also works when using the original one ([[migxResourceMediaPath? &pathTpl=`assets/resourceimages/{id}/`]]).

            Things get weird with the snippet call to BabelTranslation, even if i just add:
            $modx->log(modX::LOG_LEVEL_ERROR, 'Result: '.$modx->runSnippet('BabelTranslation', array('contextKey' => 'web', 'resourceId' => $docid)));

            ..in other words, even if if i don't use it the upload won't work.
            • discuss.answer
              • 4172
              • 5,888 Posts
              https://forums.modx.com/thread/86004/dynamic-media-source-with-migx-and-babel#dis-post-474193

              seems BabelTranslation is trying to call modResource-methods, but there isn't a modx->resource-object at this time.

              you can try to add one with:

              <?php
              
              if (!is_object($modx->resource)){
                  $modx->resource = $modx->newObject('modResource');
              }
              $docid = $modx->runSnippet('BabelTranslation', array('contextKey' => 'web', 'resourceId' => $docid));
              $modx->log(modX::LOG_LEVEL_ERROR, 'docid migxBabelResourceMediaPath: '.$docid);
              
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 20471
                • 40 Posts
                Bruno, you're a genius! Works like a charm. Thank you so much!
                  • 15362
                  • 22 Posts
                  Hello @mrtnrsl,

                  Regarding the above did you manage to solve this issue, syncing migx gallery and babel?