Filter getResources results when value of TV matches a defined criteria

Got it, but I still need those TV IDs.

ELEMENT-WorkIndex-Top10Featured = 462
ELEMENT-2019Project-WorkIndexProjectBlurb = 489
ELEMENT-2019Project-WorkIndexSelections = 463
ELEMENT-2019Project-WorkIndexCoverImage = 466

Only thing is… client now wants to have more versions of the ELEMENT-2019Project-WorkIndexCoverImage TV to suit the result as it sits on different pages… will this mess up your approach / plan?

So, now we have the blelow TVs - this is an updated set as just had to amend things to allow different images to be rendered in the output TPL based on which page the getResources call is placed on!

NOTE - ELEMENT-2019Project-WorkIndexCoverImage = 466 is no longer used.

ELEMENT-WorkIndex-Top10Featured = 462
ELEMENT-2019Project-WorkIndexProjectBlurb = 489
ELEMENT-2019Project-WorkIndexSelections = 463
ELE-2019Project-WorkIndexCoverIMG-EXPERTISE = 491
ELE-2019Project-WorkIndexCoverIMG-EXPERTISE-CAT1 = 492
ELE-2019Project-WorkIndexCoverIMG-EXPERTISE-CAT2 = 493
ELE-2019Project-WorkIndexCoverIMG-EXPERTISE-CAT3 = 494
ELE-2019Project-WorkIndexCoverIMG-EXPERTISE-CAT4 = 495
ELE-2019Project-WorkIndexCoverIMG-EXPERTISE-CAT5 = 496
ELE-2019Project-WorkIndexCoverIMG-INDUSTRY = 497
ELE-2019Project-WorkIndexCoverIMG-INDUSTRY-CAT1 = 498
ELE-2019Project-WorkIndexCoverIMG-INDUSTRY-CAT2 = 499
ELE-2019Project-WorkIndexCoverIMG-INDUSTRY-CAT3 = 500
ELE-2019Project-WorkIndexCoverIMG-INDUSTRY-CAT4 = 501
ELE-2019Project-WorkIndexCoverIMG-INDUSTRY-CAT5 = 502

Sorry…

That TV is needed… but renamed as below:

ELE-2019Project-WorkIndexCoverIMG-ALL = 466

No, it can have a different value for each page as long as it’s a single value, but I’m confused by why you want to have 12 separate TVs for a single placeholder. Can’t you just set the appropriate value for each page?

One other thing still confuses me – the value of ELEMENT-WorkIndex-Top10Featured. Does it have a different value for each resource? If not, I’d suggest making it a System Setting rather than a TV.

OK thanks. Its not a single placeholder - the CMS user can set a different image to render on the different resources the getResources call is placed on.

The TV “ELEMENT-WorkIndex-Top10Featured” has different values on the different resources the different getResources calls are placed on yes - so its never going to have the same values.

@elizabeth Once again I call for this info about the three backticks for posting code to be stickied on the front page. The forum software has a ‘code’ formatting which does not always work, and there is no acknowledgement or direction at that point.

New users of the forum do not know this info, and its not handy while being really critical. @lucy within the past couple weeks also had to inform a new user about it. It just slows things down. A month ago a user posted a great little piece of code, but had to change all the ticks in it to some other character due to uncertainty about the ticks.

There’s just no sense in telling people every week, I know every sticky makes the page longer, but…

Thanks! :slight_smile:

@nuan88 - Agreed - should be away in the little editor to render a block of code nice and easily!

1 Like

I don’t see any reason for this. Just set the image in the TV on the page with the getResources call and use [[*TvName]] instead of [[+TvName]] in the Tpl chunk.

But the page the getResources call is on is not anything to do with the value of the image TVs :wink: The IMG TVs are all called from the resources getResources is analysing… Does that make sense?

I think I might have come up with a simpler way of doing this… so, with things working as they stand, the tvFilter doesnt do anything… so…

How about I add this code to the getResources TPL chunk

[[!If?
&subject=`[[+ELEMENT-2019Project-WorkIndexSelections]]`
&operator=`contains`
&operand=`[[*id]]`
&then=`displayME`
&else=`hideME`
]]

So, getResources outputs the results it finds including the ones we dont want - we then in the TPL it outputs the results use the IF operation in the TPL chunk as above to look at the contents of TV “ELEMENT-2019Project-WorkIndexSelections” and if it CONTAINS a match of the ID of the current page the getResources call is on, then it renders “displayME” - I then use CSS to make this result visible, for any outputed results that dont have a match with the page ID and TV “ELEMENT-2019Project-WorkIndexSelections” we get a “hideME” and I hide them with CSS…

Only issue is… There is no “CONTAINS” operator in the sytax…! Is it possible to make it work??

Value of TV “ELEMENT-2019Project-WorkIndexSelections” could be “72”, could be “71,72,80” - so I need it to look and match a value between the comma seperated numbers returned…

Possible? Easier than other approach???

1 Like

BINGO!!!
Got it

[[+ELEMENT-2019Project-WorkIndexSelections:contains=`[[*id]]`:then=`showME`:else=`hideME`]]
1 Like

same issue, as with the other solutions, will also find 720 or 172, for example and will get very slow with a lot of resources

1 Like

Seems to work fine though - just as I need it… might not be the most ideal solution - but I could not get anything else to work as I needed it…

1 Like

A related question - how can I use the Output Modifier to have multiple IFs? e.g.

If ID = 729 - render the first , If ID = 731 render the 2nd image… the code below doesnt work - I guess syntax is wrong - but cant find an example of ths in the docs to follow?

[[*id:eq=`729`:then=`<img src="[[+ELE-2019Project-WorkIndexCoverIMG-EXPERTISE]]" alt="Read more about [[+pagetitle]]" title="Read more about [[+pagetitle]]" class="workindexProjectImage">`:

if:*id:eq=`731`:then=`<img src="[[+ELE-2019Project-WorkIndexCoverIMG-EXPERTISE-CAT1]]" alt="Read more about [[+pagetitle]]" title="Read more about [[+pagetitle]]" class="workindexProjectImage">`:

:else=``]]
1 Like

As long as I’ve already written it, you might as well give this a try. It will be Way faster than what you’re doing.

/* GetTopTen snippet

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

$output = '';

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

$selection = $modx->getOption('selectionValue', $scriptProperties, '', true);
$docs = $modx->resource->getTVValue($topTenTv);
$output .= '<br>Docs: ' . $docs;
/* $docs = explode(',', $docs); */

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

$criteria = array();
$criteria['modTemplateVarResource.tmplvarid'] = $selectionsTv;
$criteria['modTemplateVarResource.value:LIKE'] = '%' . $selection . '%';
$criteria['modTemplateVarResource.contentid:IN'] = $docs;

$criteria = $modx->newQuery('modTemplateVarResource', $criteria);
$criteria->limit(10);
$criteria->sortby('RAND()');
$tvrs = $modx->getCollectionGraph('modTemplateVarResource', '{"Resource":{}}', $criteria);

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

foreach($tvrs as $tvr) {
    $val = $tvr->get('value');
    if (strpos($val, ',') !== false) {
        $values = explode(',', $val);
    } else {
        $values = explode('||', $val);
    }
    if (in_array($selection, $values)) {
       $output .= "<br>Found one";
        /* 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;

It will use the value of the blurb TV on the page with the tag for all the aggregated resources.

1 Like

Thanks Bob - I will give your version a try and report back - missed this when you posted it.

1 Like

Hi Bob - Thanks once more for taking the time to have a go at a solution for this - I tried it and it doesnt appear to return anything which is odd. I don’t really understand the PHP to check what should be happened so can’t really help debug things which is a shame. Any chance you could give me a version with some commented lines of what the various aspects do?

1 Like

Here’s one mistake I made. The Tpl name needs to go in the tag:

[[GetTopTen? &selectionValue=`729` &myTpl=`ActualTplName`]]
1 Like

Thanks - still returns nothing - sorry Bob!

1 Like

Double-check the value of the variables at the top and the spelling of the chunk name and the property names in the tag.

I put a couple of debugging lines in the code above that may tell us something about where the problem is. I also commented out the first “explode” line, which should have been removed and would probably have caused the empty result.

1 Like

Thanks Bob - OK, ace - returns values now… but… :wink:

Its just returning the value of the TV ELEMENT-WorkIndex-Top10Featured - which can contain resources which dont match the resource ID of the page the getResources code / your snippet is placed on - these values should not appear in the results - as we want to filter the values of TV ELEMENT-WorkIndex-Top10Featured to only display results that have a TV ELEMENT-2019Project-WorkIndexSelections value that matches the ID of the current resource…?

1 Like