We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16430
    • 217 Posts
    Hi, I have this code to return number of all children (including subfolders) in specified parent for logged user.
    It works but It needs some optimization. For example do not include unpublished, deleteed or menuhidden parents...atc..

    Any help?
    <?php
    $profile = $modx->user->getOne('Profile');
      if ($modx->user) {  //if user logged in
      $value = 0;
      $array_ids = $modx->getChildIds($parentId,3,array('context' => 'web')); // get array of all parents
      foreach ($array_ids as &$parents) { //count child for each parent
        $value = $value + $modx->getCount('modResource', array('parent'=>$parents,'published'=>'1', 'deleted'=>'0','template'=>'1','createdby'=>$modx->user->get('id') ));
      }
    
        return $value;
      }
    • I added several features for working with children to the setPlaceholders extra ( http://modx.com/extras/package/setplaceholders ) recently. It can get a count of the immediate children for any resource, or get a field/TV from a particular child (counting from the first or last), or from a random child.

      Return the number of children for the current resource:
      [[setPlaceholders? &ph=`childC` &output=`1`]]

      Set a placeholder [[+childcount]] with the number of children the current resource's parent has:
      [[setPlaceholders? &id=`[[*parent]]` &ph=`childcount == childC`]]
      or [[setPlaceholders? &ph=`childcount == parent.childC`]]


      Or something crazy:
      [[setPlaceholders? &ph=`umm == 13.Uparent2.next3.child-2.pagetitle`]]
      Which starts at resource 13, goes to its 2nd highest level parent, skips forward 3 siblings and gets the pagetitle of that resource's 2nd-to-last child. Available in [[+umm]]. Though if you actually need that you may have bigger problems smiley

        Extras :: pThumbResizerimageSlimsetPlaceholders
      • That sounds like something I could use on this huge site I'm trying to port to Revo! I had to laugh, it was so apropos.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
        • Quote from: sottwell at Apr 09, 2013, 09:41 PM
          That sounds like something I could use on this huge site I'm trying to port to Revo! I had to laugh, it was so apropos.

          Heh. It's a bit of a strange amalgam of placeholders + a super GetResourceField (in the spirit of a classic radio/flashlight/spork combo), but I personally find it very handy and end up using it in a handful of places on nearly every site I build.

          If you have any questions about it, start up another thread and I'll try to answer them.
            Extras :: pThumbResizerimageSlimsetPlaceholders
            • 35844
            • 15 Posts
            Ralph Leo Bennett Reply #15, 9 years ago
            A bump to an old post here. I'm trying to find a good solution to using the number of children as a sorting method for 'getResources'.

            Can I use 'getResources' to sort by a snippet's output? I could then use either 'setPlaceholders' or 'getChildCount' to generate the data needed for sorting.
              • 3749
              • 24,544 Posts
              getResources doesn't have any access to the number of children, so the only way to make that work would be to create a TV containing the number of children and sort by that TV. You could write a plugin that would update the TV when you save a resource (connected to OnDocFormSave).
                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
                • 35756
                • 166 Posts
                Related to reply #14:

                Is it possible to get the amount of child-resources from more than 1 parent like this:

                [[setPlaceholders? &id=`1474,1473,1472,57,89,1483,1499` &ph=`childcount == childC`]]


                So that all child-resources get added together to achieve a total number of resources?