We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21677
    • 15 Posts
    I’m trying to get all documents in all contexts, I’ve succeeded at getting resources out of multiple contexts, but it doesn’t recurse through parents. My call looks like:

    [[!getPage?
       &elementClass=`modSnippet`
       &element=`getResources`
    
       &context=`context1,context2`
    
       &depth=`10`
       &limit=`5`
    
       &showUnpublished=`1`
       &showHidden=`1`
    
       &parents=`0`
    
       &sortby=`editedon`
    
       &tpl=`archiveListItem`
    ]]


    it grabs all resources w/o getting any of their children. I believe I have sufficiently cleared my site cache, I also took a look at the raw array, it didn’t show me anything different. If I manually put in &parents=`0,3` it does get the children of resource #3 but it doesn’t get it’s children. I’ve looked around at other posts and didn’t see a similar problem. Am I missing something?

    UPDATE: I’m now wondering if I just didn’t set up my context properly. Would this make any sense?

    UPDATE #2: So I must have coppied over my core.config.php for the new context, that was causing some other problems, but that was worked out and now we’re at the same place we were. I have thoroughly tested out the problem and it’s obvious that getResource is only able to recurse through parents within the parent context of the snippet.

    Example:
    Context#1 lists all resources from Context#1 & Context#2 on the first level only, it also recurses though all resources within Context #1. Failing to recurse through Context#2.

    Context#2 lists all resources from Context#1 & Context#2 on the first level only, it also recurses though all resources within Context #2. Failing to recurse through Context#1.

    I’ve got some other work to do, but this sounds like a bug in getResource, I’ll be looking later.

    UPDATE #3: The error exists some where in here I believe. Don’t know what else to say...?
    $children = array();
    foreach ($parents as $parent) {
        $pchildren = $modx->getChildIds($parent, $depth);
        if (!empty($pchildren)) $children = array_merge($children, $pchildren);
    }
    if (!empty($children)) $parents = array_merge($parents, $children);


    Has nobody ran into this before?

    [tt]$modx->getChildIds($parent, $depth)[/tt] w/ [tt]$parent=0[/tt] only gets children in that context, that’s why I’m not getting fully recursive results.
      • 21677
      • 15 Posts
      Solution:
      inside getResources, replace:
      $children = array();
      foreach ($parents as $parent) {
          $pchildren = $modx->getChildIds($parent, $depth);
          if (!empty($pchildren)) $children = array_merge($children, $pchildren);
      }
      if (!empty($children)) $parents = array_merge($parents, $children);

      with:
      $children = array();
      if (!empty($context)) {
        $c = explode(',',$context);
        $contexts = array();
        foreach ($c as $ctx) {
          $t = $modx->switchContext($ctx);
          foreach ($parents as $parent) {
          $pchildren = $modx->getChildIds($parent, $depth);
          if (!empty($pchildren)) $children = array_merge($children, $pchildren);
        }
        if (!empty($children)) $parents = array_merge($parents, $children);
        }
      } else {
        foreach ($parents as $parent) {
          $pchildren = $modx->getChildIds($parent, $depth);
          if (!empty($pchildren)) $children = array_merge($children, $pchildren);
      }
      if (!empty($children)) $parents = array_merge($parents, $children);
      }


      Basically I am iterating through the contexts, and switching to them, listed in the snippet call before getting the children of the parents. I didn’t know about switchContext, once I found it it was pretty obvious.

      This isn’t exactly a best-practice, I haven’t tested it against access controls or anything like that, so it very well may need some work. If you notice anything don’t be afraid to speak up.
        • 22347 ☆ A M B ☆
        • 29 Posts
        Thanks for the help on this issue... was successful in solving same issue.
          "Give a company a website, and they will be successful for now...
          Give a company a long-term web strategy, and they will be successful for a lifetime."

          Company Website: http://www.orbitalalliance.com
          Company Blog: http://www.orbitalalliance.com/blog