We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23018
    • 353 Posts
    I have a media source that relies on the id of the current resource. Its working quite nice, but as soon as I'm using it in the context of getresources or wayfinder I hit a wall.

    My media source settings:
    Base Path: [[id2path]]
    Base Url: [[id2path]]


    The id2path snippet:
    <?php
    /*
    id2path (simplified version of  for this post)
    Code "borrowed" from Bruno17s migxResourceMediaPath snippet
    */
    
    $rootfolder = $modx->getOption('rootfolder', $scriptProperties, 'id/');
    $docid = $modx->getOption('docid', $scriptProperties, '');
    
    // Find out if docid was set as a placeholder
    if (empty($docid) && $modx->getPlaceholder('docid')) {
        $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'];
            }
        }
    }
    
    return $rootfolder.$docid."/";
    ?>


    It works quite nice in templates, redactor, tinymce, filetree, etc.

    Example: [[*imageTV]] in a template for resource #4 returns:

    id/4/example.jpg

    But if I'm "using" getResouces in resource #4. I get this in return:

    7 - id/4/image1.jpg
    8 - id/4/image2.jpg
    9 - id/4/image3.jpg

    [[getResources?
       &parents=`4`
       &tpl=`tplChunk`
       &includeTVs=`1`
       &tvPrefix=``
       &prepareTVs=`1`
    ]]


    <!-- tplChunk -->
    [[+id]] - [[+imageTV]]


    I understand whats going wrong here, but I can't find a way to get around this problem.

    Setting a docid placeholder in the tpl chunk didn't work:

    7 - 7 - id/4/image1.jpg
    8 - 8 - id/4/image2.jpg
    9 - 9 - id/4/image3.jpg

    <!-- tpl Chunk -->
    [[setPH? &name=`docid`&value=`[[+id]]`]]
    [[+id]] - [[+docid]] - [[+imageTV]]


    <?php
    return $modx->setPlaceholder($name,$value)
    ?>
    


    As this can't work because getResources is preparing the content of imageTV before the chunk is processed, the only way I see to handle this is to hack getResources to set this placeholder before the template variables are "prepared".

    The only solution I have found ist stripping the path from the filename and calculating the path with a snippet.

    [[fixPath? &file=`[[+imageTV]]` &docid=`[[+id]]`]]


    <?php
       /* fixPath snippet */
       $rootfolder = $modx->getOption('rootfolder', $scriptProperties, 'id/');
       $docid        = $modx->getOption('docid', $scriptProperties, $id);
       $file            = $modx->getOption('file', $scriptProperties, false);
    
       if(!$file OR empty($file)) return;
    
       $basename = basename($file);
       $fixPath      = $rootfolder.$docid."/".$basename;
    
       return $fixPath;
    ?>


    Its UGLY.

    Any advice how to handle this is welcome.

    Regards,

    pepebe [ed. note: pepebe last edited this post 12 years, 7 months ago.]
      Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe