0

How can I use getResources to get the parent resource from several nodes in a resource tree ~only once~ i.e. I have a getResources call:

[[!getResources?  
    &parents=`738,746,1222, 748, 1216, 805, 806, 807, 3401`
    &tpl=`SecondaryUpdatesHomePageTpl`  
    &limit=`3`  
    &includeTVs=`1`
    &processTVs=`1`  
    &hideContainers=`1`  
    &includeContent=`0`   
    &depth=`1`  
    &sortby=`{"createdon":"desc"}`
]]

Where the &parents ids are the trees to be searched, there may be several new resources in each parent. I need get resources to return the parent resource of the &parents items 'but only once'

For example if parents 738, 748. 807 & 3401 each have 4 or 5 new resources under them, I don't want 738 getting returned 3 times, I need ~the parents of~ 738, 748 & 807 returned.

Any thoughts on how to do this? [modx revolution 2.2.12]

Sean Kimball
  • 4,506
  • 9
  • 42
  • 73

2 Answers2

0

After looking at this for a while i guess you basically just have to do another getResources call with the &resources param set to a modified output of the first getResources call. Note that i removed the includeTVs from the nested getResources call.

Change the TPL in your getResources to output the parent followed by a comma, ([[+parent]],) basically generating a comma separated list of ID's. Feed the output of your getResources call into another one that has the correct TPL (SecondaryUpdatesHomePageTpl)

[[!getResources? 
  &resources=`[[!getResources?  
    &parents=`738,746,1222, 748, 1216, 805, 806, 807, 3401`
    &tpl=`CSVListOfParentsTPL`  
    &limit=`3`  
    &hideContainers=`1`  
    &depth=`1`  
    &sortby=`{"createdon":"desc"}`
  ]]` 
  &tpl=`SecondaryUpdatesHomePageTpl`
  &includeTVs=`1`
  &processTVs=`1`  
]]

Nesting like this is clearly not the most efficient solution, the alternative would be writing your own custom snippet that makes a $modx->query out of more or less the same parameters your giving the getResources, but fetching the parents directly instead of doing a second trip to get them.

  • Looks like that will still return the same parent multiple times. I'm going to have to use runSnippet on getresources and then filter the results myself. Thanks though. – Sean Kimball Apr 07 '14 at 20:58
0

Try to use the new snippet:

[[unikids? &input=`1,5,6` $depth=`5`]]

and snipet code:

<?php

$array_big = array();
$inputids = explode(",",$input);
foreach($inputids as $inputid) {

 $array_ids = $modx->getChildIds($inputid,$depth,array('context' => 'web'));

        /// add to master array
        $array_big = array_merge ($array_big, $array_ids);
   } 

   $output = implode(",",array_unique($array_big));

 return $output;

?>

this snipet outputs unique ids to your getResources parametr :

[[!getResources? 
  &resources=`[[unikids? &input=`1,5,6` $depth=`5`]]` 
  &tpl=`SecondaryUpdatesHomePageTpl`
  &includeTVs=`1`
  &processTVs=`1`  
]]