We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    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?

      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 46654
      • 14 Posts
      I don't think you'll get the functionality you want with getResources. (someone correct me if i'm wrong.)

      I wrote a script that should do what you want, although its probably expensive since i'm pulling in every field from the resource. (this means you could use every field if you wanted to...)

      If you need to tone down on what is called, you can modify the script below only get things you need.

      getResourceParents (snippet)

      <?
      $children = explode(',', $parents);
      $parentIDs = [];
      foreach($children as $key => $value){
          $temp = $modx->getObject('modResource', $value);
          $parentIDs[$key] .= $temp->get('parent');
      }
      $tmp = [];
      foreach ($parentIDs as $key => $parent){
           array_push($tmp, $modx->getObject('modResource', $parent)->_fields); 
      }
      
      function subval_sort($a,$subkey) {
          foreach($a as $k=>$v) {
              $b[$k] = strtolower($v[$subkey]);
          }
          arsort($b);
          foreach($b as $key=>$val) {
              $c[] = $a[$key];
          }
          return $c;
      }
      
      $tmp = array_unique($tmp, SORT_REGULAR);
      $tmp = subval_sort($tmp,'createdon'); 
      
      foreach($tmp as $key => $temp){
           $o .= $modx->getChunk($tpl, $temp); 
      }
      
      return $o;
      


      Snippet Call

      [[!getResourceParents?  
          &children=`40,11,22,28`
          &tpl=`chunk.tpl`    
      
      ]]
      


      chunk.tpl

      [[+id]]<br>
      [[+alias]]<br>
      


      Just some notes if this does happen to work for you:
      1. &children needs to have no spaces, since i'm taking the value and making an array from it.
      2. The chunk can use any field from the database table as i've noted above. So you can use pagetitle, description, createdon, content, etc.

      Note: i'm not a php pro, so you'll probably get better advice from someone else tongue
        • 3749
        • 24,544 Posts
        I think this might make the code simpler, though your way may be faster:

        foreach($children as $child) {
           $parentObj = $child->getOne('Parent');
        }
          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