We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17837
    • 8 Posts
    Hi,

    I'm creating my portfolio site using MODX Revo. I want to have a section named "Related Works" on every page displaying single project. I have a TV named "Category" (with Input Type set to Checkbox, Output Type set to Delimiter, where delimiter is ", "). I use this Category TV on every project page to choose which categories a particular project belongs too.
    I want to add these related works using pdoTools (using pdoResources), where tpl chunk for pdoResources looks like this:

    [[+tv.Category:contains=`[[*Category:replace=`, ==`:or:contains=``]]`:then=`
    HERE COMES THE CODE THAT DISPLAYS A RELATED PROJECT THUMBNAIL
    `:else=``]]
    


    Let's say that I have a project with two categories assigned - "blue" and "red". I want to display only projects which [[+tv.Category]] contains at least one of the categories assigned to current project page. That's why I use "replace" modifier to add additional ":or:contains" modifiers between multiple categories in string coming from [[*Category]]. I think it should change default [[*Category]] output:
    blue, red
    to:
    blue`:or:contains=`red
    which in turn should make the first line of code look like this:
    [[+tv.Category:contains=`blue`:or:contains=`red`:then=`
    


    Unfortunately it doesn't work, no related projects are displayed. What is wrong with my code?

    Thanks in advance for any help.

    This question has been answered by Maciej Krupa. See the first response.

    [ed. note: Maciej Krupa last edited this post 10 years, 4 months ago.]
      • 3749
      • 24,544 Posts
      One problem is that the contains: filter doesn't process or conditions (unless you've re-written it). Also, I'm not sure *category is available at the time the Tpl chunk is processed (it may be).

      I think you really need a custom snippet. [ed. note: BobRay last edited this post 10 years, 4 months ago.]
        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
        • 3749
        • 24,544 Posts
        I'm not sure I understand what you want to output, so this is a guess:

        In the Tpl chunk:

        [[!ShowRelated? &tv=`[[+tv.Category]]`]]


        /* ShowRelated snippet */
        $tv = $modx->getOption('tv', $scriptProperties, '', true);
        $category = $modx->resource->get('Category');
        
        $cats = explode(',', $category);
        
        
        /* collect the categories contained in the TV */
        $found = array();
        foreach($cats as $cat) {
           if (strpos($tv, trim($cat)) !== false) {
              $found[] = $cat;
           }
        }
        
        /* Assuming you want to output a thumbnail for each term found */
        foreach($found as $k => $v) {
           $output .= $v . ' Code to display project thumbnail';
        }
        
        return $output;

          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
          • 17837
          • 8 Posts
          Looks like contains: filter does process "or", because after changing delimiter in Category TV output type to:
          `:or:contains=`

          and changing first line of code to:
          [[+tv.Category:contains=`[[*Category]]`:then=`

          it started to work.

          Anyway now I have a slightly different problem - I don't know how to limit the total number of generated related projects. I can't do it on pdoResources level because comparison of categories is executed in tpl chunk where all resources are sent no matter if they have similar categories or not.

          So I suppose my solution is rather bad and not elegant idea. Is there any other way to add related projects to my pages, based on TV values? I'm not obsessed with using pdoResources, maybe getResources will be a better/more flexible solution? Or any other addon?

          Update: Thank you BobRay for the snippet. You replied while I was writing my post. I will try your solution.
            • 3749
            • 24,544 Posts
            I don't think there's any way to limit the number without using a custom snippet. In any event, doing it with that many conditional output modifiers would be *way* slower and less efficient.

            If the snippet works, it would be very easy to add a &limit property to set the number.
              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
              I would suggest to use tagger for what you are after.
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
              • discuss.answer
                • 17837
                • 8 Posts
                I found a component named getRelated and it solves my problem. Thanks for your effort.

                Update & final working solution:

                I realized that getRelated works fine but lacks one important feature - there's no way to display a random list of related projects. When I limit output to let's say 2 projects, I will always see two recent projects. This would cause older project to permanently disappear from related projects list. GetRelated lacks sorting function (its tvSort and tvSortDir properties are used for sorting query, not output). After some trials and errors I finally found a solution that displays randomly selected, limited to desired amount, set of related projects based on project categories set in TV. I want to show how I did this because others may have the same problem in the future.

                I used a combo of getRelated (https://modx.com/extras/package/getrelated) and pdoResources (part of pdoTools: https://modx.com/extras/package/pdotools).
                First I put that code into my project page template:
                [[!pdoResources?
                &parents=`ID OF MY PORTFOLIO PROJECTS' CONTAINER`
                &depth=`0`
                &limit=`2`
                &resources=`[[$Related-id-unlimited-list]]`
                &sortby=`RAND()`
                &tpl=`TPL FOR THUMBNAILS`
                &includeTVs=`LIST OF TVs I WILL NEED FOR GENERATING THUMBNAILS`
                ]]
                


                pdoResources generates two thumbnails (&limit=`2`), randomly selected (&sortby=`RAND()`) from a longer list of IDs of related resources taken from Related-id-unlimited-list chunk.

                Here is [[$Related-id-unlimited-list]] content:
                [[getRelated?
                &fields=`tv.Category`
                &parents=`ID OF MY PORTFOLIO PROJECTS' CONTAINER`
                &limit=`0`
                &noResults=``
                &tplOuter=`outer-tpl`
                &tplRow=`row-tpl`
                ]]
                


                getRelated outputs a comma separated list of IDs of related projects, by comparing contents of Category TV. Here I put &limit=`0` to get a list of all related resources. pdoResources from the code above will limit it later.

                Content of outer-tpl chunk:
                [[+wrapper]]
                


                Content of row-tpl chunk:
                [[+id]],
                


                Let's say that for my current project, getRelated finds four related resources with IDs 5, 8, 10, 15. Output from Related-id-unlimited-list chunk is "5, 8, 10, 15, " and it is passed to pdoResources 5th line of code:
                &resources=`5, 8, 10, 15, `
                


                And now pdoResources will randomly select two IDs from this list and generate thumbnails. So every time I refresh page I see a different set of two thumbnails of related projects, even old ones that would disappear when using only getRelated alone. [ed. note: Maciej Krupa last edited this post 10 years, 4 months ago.]