We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51347
    • 82 Posts
    Hello MODx Forum,

    i need your help with a snippet again.

    I have a resource with the tv="mynumber", tvid="410", value="12345"

    I created a media source, which should automatically have the path "assets/images/12345" (even if the resource is shown on other sites via PDOresources).


    My Media Source basePath: assets/images/[[getmyTV]]
    My Media Source baseURL: assets/images/[[getmyTV]]

    mySnippet getmyTV (suggested by BobRay):
    <?php
    $resid = $modx->resource->get('id');
    $page = $modx->getObject('modResource', $resid);
    return $modx->resource->getTVValue('mynumber');
    


    Unfortunately, i get the ERROR:
    Code: 200 OK
    Fatal error: Call to a member function get() on a non-object in /.../core/cache/includes/elements/modsnippet/100.include.cache.php on line 2



    Bruno suggested this solution, but it didnt work:
    [[migxResourceMediaPath? &tvname=`identnr` &pathTpl=`assets/images/{tv_value}/`]] 

    https://github.com/Bruno17/MIGX/commit/e611ca75e581c7a90c1254f802903ba74170f9ad


    Please help me!

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

      • 24629
      • 370 Posts
      Hi,

      I don't see what you want to do with $resid and $page since you only return the MyNumber TV.
      I'm also not sure you can have dynamic Mediasource paths if thats what your aiming for

      RDG
        • 3749
        • 24,544 Posts
        The question is whether the tag in the Media Source tag will be interpreted at run time (or ever). As Ralph suggests, I'm almost sure it won't.

        Do you really need a dynamic Media Source here? If the Media Source for the user is a particular directory (say assets/images/), you could use the TV as the final path to the image in the img tag.
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 4172
          • 5,888 Posts
          your snippet will not work. Many missing parts.
          You will need a snippet like

          migxResourceMediaPath to get it working at Manager and at Frontend.

          Did you add my new additions?
          What are your issues with migxResourceMediaPath?

          Manager, Frontend - Issues?
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 51347
            • 82 Posts
            Thank you for all your answer!

            Short info about my idea:
            In the future there will be many users and every user will only have access to one resource. Therefore every resource needs its own media source, so that the users dont have access to the files of other users. Because of the large number of users the media sources need to be generated automatically.


            My try with migxResourceMediaPath:

            basePath: [[migxResourceMediaPath? &tvname=`mynumber` &pathTpl=`assets/images/{tv_value}/`]]
            baseUrl: [[migxResourceMediaPath? &tvname=`mynumber` &pathTpl=`assets/images/{tv_value}/`]]

            When i try upload a picture: ERROR (cant find the folder "{tv_value}"
            Das angegebene Verzeichnis ist kein Verzeichnis.: /assets/images/{tv_value}/

            Here is my changes:
            core/components/migx/elements/snippets/migxresourcemediapath.snippet.php
            <?php
            /**
             * @name migxResourceMediaPath
             * @description Dynamically calculates the upload path for a given resource
             * 
             * This Snippet is meant to dynamically calculate your baseBath attribute
             * for custom Media Sources.  This is useful if you wish to shepard uploaded
             * images to a folder dedicated to a given resource.  E.g. page 123 would 
             * have its own images that page 456 could not reference.
             *
             * USAGE:
             * [[migxResourceMediaPath? &pathTpl=`assets/businesses/{id}/`]]
             * [[migxResourceMediaPath? &pathTpl=`assets/test/{breadcrumb}`]]
             * [[migxResourceMediaPath? &pathTpl=`assets/test/{breadcrumb}` &breadcrumbdepth=`2`]]
             *
             * PARAMETERS
             * &pathTpl string formatting string specifying the file path. 
             *		Relative to MODX base_path
             *		Available placeholders: {id}, {pagetitle}, {parent}
             * &docid (optional) integer page id
             * &createFolder (optional) boolean whether or not to create
             */
            $pathTpl = $modx->getOption('pathTpl', $scriptProperties, '');
            $docid = $modx->getOption('docid', $scriptProperties, '');
            $createfolder = $modx->getOption('createFolder', $scriptProperties, false);
            $tvname = $modx->getOption('tvname', $scriptProperties, '');
            $path = '';
            $createpath = false;
            
            if (empty($pathTpl)) {
                $modx->log(MODX_LOG_LEVEL_ERROR, '[migxResourceMediaPath]: pathTpl not specified.');
                return;
            }
            
            if (empty($docid) && $modx->getPlaceholder('mediasource_docid')) {
                // placeholder was set by some script
                // warning: the parser may not render placeholders, e.g. &docid=`[[*parent]]` may fail
                $docid = $modx->getPlaceholder('mediasource_docid');
            }
            
            if (empty($docid) && $modx->getPlaceholder('docid')) {
                // placeholder was set by some script
                // warning: the parser may not render placeholders, e.g. &docid=`[[*parent]]` may fail
                $docid = $modx->getPlaceholder('docid');
            }
            if (empty($docid)) {
            
                //on frontend
                if (is_object($modx->resource)) {
                    $docid = $modx->resource->get('id');
                }
                //on backend
                else {
                    $createpath = $createfolder;
                    // We do this to read the &id param from an Ajax request
                    $parsedUrl = parse_url($_SERVER['HTTP_REFERER']);
                    parse_str($parsedUrl['query'], $parsedQuery);
            
                    if (isset($parsedQuery['amp;id'])) {
                        $docid = (int)$parsedQuery['amp;id'];
                    } elseif (isset($parsedQuery['id'])) {
                        $docid = (int)$parsedQuery['id'];
                    }
                }
            }
            
            if (empty($docid)) {
                $modx->log(MODX_LOG_LEVEL_ERROR, '[migxResourceMediaPath]: docid could not be determined.');
                return;
            }
            
            if ($resource = $modx->getObject('modResource', $docid)) {
                $path = $pathTpl;
                $ultimateParent = '';
                if (strstr($path, '{breadcrumb}') || strstr($path, '{ultimateparent}')) {
                    $parentids = $modx->getParentIds($docid);
                    $breadcrumbdepth = $modx->getOption('breadcrumbdepth', $scriptProperties, count($parentids));
                    $breadcrumbdepth = $breadcrumbdepth > count($parentids) ? count($parentids) : $breadcrumbdepth;
                    if (count($parentids) > 1) {
                        $parentids = array_reverse($parentids);
                        $parentids[] = $docid;
                        $ultimateParent = $parentids[1];
                    } else {
                        $ultimateParent = $docid;
                        $parentids = array();
                        $parentids[] = $docid;
                    }
                }
            
                if (strstr($path, '{breadcrumb}')) {
                    $breadcrumbpath = '';
                    for ($i = 1; $i <= $breadcrumbdepth; $i++) {
                        $breadcrumbpath .= $parentids[$i] . '/';
                    }
                    $path = str_replace('{breadcrumb}', $breadcrumbpath, $path);
                }
            
            
                if (!empty($tvname)){
                $path = str_replace('{tv_value}', $resource->getTVValue($tvname), $path);    
                }
                $path = str_replace('{id}', $docid, $path);
                $path = str_replace('{pagetitle}', $resource->get('pagetitle'), $path);
                $path = str_replace('{alias}', $resource->get('alias'), $path);
                $path = str_replace('{parent}', $resource->get('parent'), $path);
                $path = str_replace('{context_key}', $resource->get('context_key'), $path);
                $path = str_replace('{ultimateparent}', $ultimateParent, $path);
                if ($template = $resource->getOne('Template')) {
                    $path = str_replace('{templatename}', $template->get('templatename'), $path);
                }
                if ($user = $modx->user) {
                    $path = str_replace('{username}', $modx->user->get('username'), $path);
                }
            
                $fullpath = $modx->getOption('base_path') . $path;
            
                if ($createpath && !file_exists($fullpath)) {
            
                    $permissions = octdec('0' . (int)($modx->getOption('new_folder_permissions', null, '755', true)));
                    if (!@mkdir($fullpath, $permissions, true)) {
                        $modx->log(MODX_LOG_LEVEL_ERROR, sprintf('[migxResourceMediaPath]: could not create directory %s).', $fullpath));
                    } else {
                        chmod($fullpath, $permissions);
                    }
                }
            
                return $path;
            } else {
                $modx->log(MODX_LOG_LEVEL_ERROR, sprintf('[migxResourceMediaPath]: resource not found (page id %s).', $docid));
                return;
            }
            



            core/components/migx/elements/snippets/snippets.1.json
            [
              {
                "id":1,
                "source":0,
                "property_preprocess":false,
                "name":"getImageList",
                "description":"",
                "editor_type":0,
                "category":0,
                "cache_type":0,
                "locked":false,
                "properties":[
                  
                ],
                "moduleguid":"",
                "static":false,
                "static_file":"",
                "filename":"getimagelist.snippet.php",
                "category_name":"MIGX"
              },
              {
                "id":2,
                "source":0,
                "property_preprocess":false,
                "name":"migxGetRelations",
                "description":"",
                "editor_type":0,
                "category":0,
                "cache_type":0,
                "locked":false,
                "properties":null,
                "moduleguid":"",
                "static":false,
                "static_file":"",
                "filename":"migxgetrelations.snippet.php",
                "category_name":"MIGX"
              },
              {
                "id":4,
                "source":0,
                "property_preprocess":false,
                "name":"migx",
                "description":"",
                "editor_type":0,
                "category":0,
                "cache_type":0,
                "locked":false,
                "properties":null,
                "moduleguid":"",
                "static":false,
                "static_file":"",
                "filename":"migx.snippet.php",
                "category_name":"MIGX"
              },
              {
                "id":7,
                "source":0,
                "property_preprocess":false,
                "name":"migxLoopCollection",
                "description":"",
                "editor_type":0,
                "category":0,
                "cache_type":0,
                "locked":false,
                "properties":null,
                "moduleguid":"",
                "static":false,
                "static_file":"",
                "filename":"migxloopcollection.snippet.php",
                "category_name":"MIGX"
              },
              {
                "id":34,
                "source":0,
                "property_preprocess":false,
                "name":"migxResourceMediaPath",
                "description":"",
                "editor_type":0,
                "category":0,
                "cache_type":0,
                "locked":false,
                "properties":[ ],
                "moduleguid":"",
                "static":false,
                "static_file":"",
                "filename":"migxresourcemediapath.snippet.php",
                "category_name":"MIGX"
              },
              {
                "id":35,
                "source":0,
                "property_preprocess":false,
                "name":"migxImageUpload",
                "description":"",
                "editor_type":0,
                "category":0,
                "cache_type":0,
                "locked":false,
                "properties":null,
                "moduleguid":"",
                "static":false,
                "static_file":"",
                "filename":"migximageupload.snippet.php",
                "category_name":"MIGX"
              },
              {
                "id":44,
                "source":0,
                "property_preprocess":false,
                "name":"migxChunklistToJson",
                "description":"",
                "editor_type":0,
                "category":0,
                "cache_type":0,
                "locked":false,
                "properties":null,
                "moduleguid":"",
                "static":false,
                "static_file":"",
                "filename":"migxchunklisttojson.snippet.php",
                "category_name":"MIGX"
              },
              {
                "id":51,
                "source":0,
                "property_preprocess":false,
                "name":"migxSwitchDetailChunk",
                "description":"",
                "editor_type":0,
                "category":0,
                "cache_type":0,
                "locked":false,
                "properties":null,
                "moduleguid":"",
                "static":false,
                "static_file":"",
                "filename":"migxswitchdetailchunk.snippet.php",
                "category_name":"MIGX"
              },
              {
                "id":52,
                "source":0,
                "property_preprocess":false,
                "name":"getSwitchColumnCol",
                "description":"",
                "editor_type":0,
                "category":0,
                "cache_type":0,
                "locked":false,
                "properties":null,
                "moduleguid":"",
                "static":false,
                "static_file":"",
                "filename":"getswitchcolumncol.snippet.php",
                "category_name":"MIGX"
              },
              {
                "id":53,
                "source":0,
                "property_preprocess":false,
                "name":"getDayliMIGXrecord",
                "description":"",
                "editor_type":0,
                "category":0,
                "cache_type":0,
                "locked":false,
                "properties":null,
                "moduleguid":"",
                "static":false,
                "static_file":"",
                "filename":"getdaylimigxrecord.snippet.php",
                "category_name":"MIGX"
              },
              {
                "id":59,
                "source":0,
                "property_preprocess":false,
                "name":"filterbytag",
                "description":"",
                "editor_type":0,
                "category":0,
                "cache_type":0,
                "locked":false,
                "properties":null,
                "moduleguid":"",
                "static":false,
                "static_file":"",
                "filename":"filterbytag.snippet.php",
                "category_name":"MIGX"
              },
              {
                "id":88,
                "source":0,
                "property_preprocess":false,
                "name":"migxObjectMediaPath",
                "description":"",
                "editor_type":0,
                "category":0,
                "cache_type":0,
                "locked":false,
                "properties":null,
                "moduleguid":"",
                "static":false,
                "static_file":"",
                "filename":"migxobjectmediapath.snippet.php",
                "category_name":"MIGX"
              },
              {
                "id":156,
                "source":0,
                "property_preprocess":false,
                "name":"exportMIGX2db",
                "description":"",
                "editor_type":0,
                "category":0,
                "cache_type":0,
                "locked":false,
                "properties":null,
                "moduleguid":"",
                "static":false,
                "static_file":"",
                "filename":"exportmigx2db.snippet.php",
                "category_name":"MIGX"
              },
              {
                "id":161,
                "source":0,
                "property_preprocess":false,
                "name":"preparedatewhere",
                "description":"",
                "editor_type":0,
                "category":0,
                "cache_type":0,
                "locked":false,
                "properties":null,
                "moduleguid":"",
                "static":false,
                "static_file":"",
                "filename":"preparedatewhere.snippet.php",
                "category_name":"MIGX"
              },
              {
                "id":168,
                "source":0,
                "property_preprocess":false,
                "name":"migxJsonToPlaceholders",
                "description":"",
                "editor_type":0,
                "category":0,
                "cache_type":0,
                "locked":false,
                "properties":null,
                "moduleguid":"",
                "static":false,
                "static_file":"",
                "filename":"migxjsontoplaceholders.snippet.php",
                "category_name":"MIGX"
              },
              {
                "id":185,
                "source":0,
                "property_preprocess":false,
                "name":"migxGetCollectionTree",
                "description":"",
                "editor_type":0,
                "category":0,
                "cache_type":0,
                "locked":false,
                "properties":null,
                "moduleguid":"",
                "static":false,
                "static_file":"",
                "filename":"migxgetcollectiontree.snippet.php",
                "category_name":"MIGX"
              },
              {
                "id":216,
                "source":0,
                "property_preprocess":false,
                "name":"migxIsNewObject",
                "description":"",
                "editor_type":0,
                "category":0,
                "cache_type":0,
                "locked":false,
                "properties":null,
                "moduleguid":"",
                "static":false,
                "static_file":"",
                "filename":"migxisnewobject.snippet.php",
                "category_name":"MIGX"
              }
            ]
            


            Media source configuration
            [ed. note: joe-petts last edited this post 10 years, 3 months ago.]
              • 4172
              • 5,888 Posts
              you did also change the snippet itself or just that file?

              you need to change just the snippet itself, not the file. The file is only used on package-installation.
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 51347
                • 82 Posts
                My fail. I see the picture now in the resource self, but it doesn't work with the PDO like this:

                PDO:
                [[!pdoResources? &parents=`12` &tpl=`mytpl`]]
                


                "mytpl" Chunk
                <div class="pic">
                   <img src="[[!lingua.getValue? &id=`[[+id]]` &field=`picture`]] ">
                </div>
                


                (media source on tv "picture")

                Result in HTML:
                <div class="pic">
                   <img src="">
                </div>
                


                Any idea?
                  • 4172
                  • 5,888 Posts
                  In this case, you will need to add the path by yourself

                  something like that, if it is in a getResources-tpl:

                  <img src="assets/images/[[+tv.mynumber]]/[[!lingua.getValue? &id=`[[+id]]` &field=`picture`]] ">
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 4172
                    • 5,888 Posts
                    I see, you are getting nothing, then is something wrong with that one:

                    [[!lingua.getValue? &id=`[[+id]]` &field=`picture`]]


                    if the TV has a value, this should return something.
                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 51347
                      • 82 Posts
                      I tested the tv's again:

                      [[!lingua.getValue? &id=`[[+id]]` &field=`mynumber`]]


                      I get the right value: 12345

                      [[!lingua.getValue? &id=`[[+id]]` &field=`picture`]]


                      I get this path: assets/images//myimage.jpg

                      The question is now, it is possible to combine this both values in:

                      assets/images/12345/myimage.jpg ?