Filter getResources results when value of TV matches a defined criteria

Thanks Dimmy - just cant get it to work - tried all sorts

1 Like

I’ve re-written the query and a little of the code, and added more debugging code. You no longer need the ID in the tag. I’m curious to know what it produces, especially the “Found one” line, which now includes the ID of the resource being processed.

/********************************************************* */
/* GetTopTen snippet

Usage: [[GetTopTen? &myTpl=`ActualTplName`]]
*/

$output = '';

$topTenTv = 462;
$blurbTv = 489;
$selectionsTv = 463;
$imageTv = 466;

$selection = $modx->resource->get('id');
$docs = $modx->resource->getTVValue($topTenTv);
$output .= '<br>Docs: ' . $docs;

/* Set ProjectBlurb placeholder */
$val = $modx->resource->getTVValue($blurbTv);
$placeholders['ELEMENT-2019Project-WorkIndexProjectBlurb'] = $val;

$criteria = $modx->newQuery('modTemplateVarResource');
$criteria->where(array(
    'modTemplateVarResource.contentid:IN' => $docs,
    'modTemplateVarResource.tmplvarid' => $selectionsTv,
    'modTemplateVarResource.value:LIKE' => '%' . $selection . '%',
));

$criteria->limit(10);
$criteria->sortby('RAND()');

$tvrs = $modx->getCollectionGraph('modTemplateVarResource', '{"Resource":{}}', $criteria);

$output .= "<br>TVR Count: " . count($tvrs);

foreach ($tvrs as $tvr) {
    /** @var $tvr modTemplateVarResource */
    $val = $tvr->get('value');
    if (strpos($val, ',') !== false) {
        $values = explode(',', $val);
    } else {
        $values = explode('||', $val);
    }
    if (in_array($selection, $values)) {
        $output .= "<br>Found one -- ID: " .  $tvr->Resource->get('id');
        /* Set resource placeholders */
        $placeholders = $tvr->Resource->toArray();

        /* Set image placeholder */
        $val = $tvr->Resource->getTVValue($imageTv);
        $placeholders['ELEMENT-2019Project-WorkIndexCoverImage'] = $val;

        /* Add results to output */
        $output .= $modx->getChunk('myTpl', $placeholders);
    }
}
return $output;
1 Like

Thanks Bob - will give it a go and let you know

1 Like

Thanks once more Bob - I’ve added the new snippet code and tested and afraid it still returns all of the resource IDs including those that it should be hiding… sorry!

Output is:

Docs: 741,750,768,781
TVR Count: 0
1 Like

Actually, no. It’s just showing the contents of the TopTen TV for the current page. Actually, it’s not returning anything – the query is coming up empty (0 results). I suspect that none of those four documents has the ID of the current page in their Selections TV (ID 463) value.

1 Like

Hi bob - The sad thing is - I’m afraid they do:

So - as an example of the actual code in place as I am using it, with getResources call on page ID ‘729’:

TV “ELEMENT-WorkIndex-Top10Featured” returns ‘741,750,768,781’

Then looking into these resources:

Resource 741 TV “ELEMENT-2019Project-WorkIndexSelections” values are = ‘779,729,731’
Resource 750 TV “ELEMENT-2019Project-WorkIndexSelections” values are = ‘779,729,733’
Resource 768 TV “ELEMENT-2019Project-WorkIndexSelections” values are = ‘779,729,733,751’
Resource 781 TV “ELEMENT-2019Project-WorkIndexSelections” values are = ‘779,751’

So…

GetResources should only output resources: 741,750 and 768
It should NOT output 781 (as it doesn’t have the value ‘729’ in the TV “ELEMENT-2019Project-WorkIndexSelections”)

But… it does get included by getResources - but nothing gets outputted by your code - that’s why I am so baffled by this!

1 Like

Doh. I’m an idiot. Somehow the line that transforms $docs into an array got deleted or left out.

Right below this line:

$docs = $modx->resource->getTVValue($topTenTv);

Add this:

$docs = explode(',', $docs);

If that doesn’t work, try this:

$docs = explode('||', $docs);

I think the first one is correct, even though it’s || in the DB because getTVValue() will convert it to a comma.

1 Like

Bingo! That now outputs the right values!

Docs: Array
TVR Count: 3
Found one -- ID: 768
Found one -- ID: 741
Found one -- ID: 750

Is what I see - which is correct! So, how do I mod it, so it just outputs a comma separated list of ids?

Can I ask why you want that comma-separated list?

Doesn’t the snippet output the resource fields in your Tpl chunk for the selected resources?

Hi Bob - I need a comma separated list of values so I can set getResources to use these as the results to display. So using the parameter below…

&resources=`COMMA SEPARATED LIST OF VALUES HERE`

The point of the snippet is to replace getResources. The snippet should give you the display that getResources would have given you.

It’s kind of wacky (i.e., slow and inefficient) to have the snippet get all the resources and TVs, and then have getResources get them all over again in a less efficient fashion.

Hi Bob - OK, didnt realise it was a replacement for getResources - I thought it was just to be used as a snippet to get the relevant resource IDs to sit into the

&resources

parameter… Is it possible to mod it to do that?

The output I get on the front end is just as below…?

Docs: Array
TVR Count: 3
Found one -- ID: 750
Found one -- ID: 768
Found one -- ID: 741

Do you have a chunk named myTpl? If not, change the name of the chunk near the end of the snippet in this line:

$output .= $modx->getChunk('myTpl', $placeholders);

to the name of the chunk you used for getResources.

Thanks Bob - yep, that worked - commented out all the other output bits - last issue… lol! is that I have another snippet shortening the leg work on the various images that this TPL displays - and it appears this new snippet is messing up the fetching of the relevant image based on your approach. Snippet for images is as below:

Template:

<img src="[[!ProjectCoverIMGSelector]]" alt="Read more about [[+pagetitle]]" title="Read more about [[+pagetitle]]" class="workindexProjectImage">

ProjectCoverIMGSelector Snippet:

<?php
$id = $modx->resource->get('id');
$targetArray = [
729 => 'ELE-2019Project-WorkIndexCoverIMG-EXPERTISE',
731 => 'ELE-2019Project-WorkIndexCoverIMG-EXPERTISE-CAT1',
732 => 'ELE-2019Project-WorkIndexCoverIMG-EXPERTISE-CAT2',
733 => 'ELE-2019Project-WorkIndexCoverIMG-EXPERTISE-CAT3',
734 => 'ELE-2019Project-WorkIndexCoverIMG-EXPERTISE-CAT4',
735 => 'ELE-2019Project-WorkIndexCoverIMG-EXPERTISE-CAT5',
730 => 'ELE-2019Project-WorkIndexCoverIMG-INDUSTRY',
736 => 'ELE-2019Project-WorkIndexCoverIMG-INDUSTRY-CAT1',
737 => 'ELE-2019Project-WorkIndexCoverIMG-INDUSTRY-CAT2',
738 => 'ELE-2019Project-WorkIndexCoverIMG-INDUSTRY-CAT3',
739 => 'ELE-2019Project-WorkIndexCoverIMG-INDUSTRY-CAT4',
740 => 'ELE-2019Project-WorkIndexCoverIMG-INDUSTRY-CAT5',
2   => 'ELE-2019Project-WorkIndexCoverIMG-ALL',
779 => 'ELE-2019Project-WorkIndexCoverIMG-ALL'
];
if (isset($targetArray[$id])){
return $modx->getPlaceholder($targetArray[$id]);
}
1 Like

Wow, do you really have 15 separate TVs to display a single image on the page? I’ve got to believe there’s a more efficient method. Would it be possible to just put the image path in one TV (maybe an @INHERIT TV) that had different values on different pages?

FWIW, I don’t see any reason why either snippet would interfere with the other.

I could integrate your snippet into mine, for a little better efficiency if you like. You’d just have a single placeholder for the Image that the snippet would set based on the array.

1 Like

Thanks for the offer Bob - but as I have another getResources call below this new version you are creating - I want to keep the IMG loading snippet as it is so things work as they are. Odd how the IMG is not being pulled in - the src is blank in the front end when I inspect it…

1 Like

Don’t worry Bob - I’ve worked it out :wink:

1 Like

Glad to hear it. :slight_smile:

1 Like

Thanks Bob. I am using your snippet within the getResources call to give me the list of resources to output and it works very well - thanks!

Last pain on this code is that within the infinite scroll getPage code I have for the rest of the results of resources - having an issue where using sortBy Rand messes things up and it lists the same results as per support note here > https://community.modx.com/t/getpage-infinite-scroll-duplicate-results-problem/1922/4

Any ideas on that one?

1 Like

Sorry, no clue. :frowning: