We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    The code above seems like overkill to me (and it makes me nervous to use PHP reserved words as variable names):

    [[!HasChildren? &id = `23`]]



    <?php
    $document = $modx->getObject('modResource', $id);
    
    if($document){
        return $document->hasChildren()? 'hasChildren' : 'noChildren';
    }
    
    return '';


    or, if you want to use isfolder instead:

    <?php
    $document = $modx->getObject('modResource', $id);
    
    if($document){
        return $document->get('isfolder')? 'hasChildren' : 'noChildren';
    }
    
    return '';
      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
      • 5340
      • 1,624 Posts
      I agree. Simple is better smiley

        • 16337
        • 44 Posts
        thats elegant BobRay wink thanks!
          • 16337
          • 44 Posts
          Quote from: BobRay at May 12, 2011, 04:42 AM

          The code above seems like overkill to me (and it makes me nervous to use PHP reserved words as variable names):

          [[!HasChildren? &id = `23`]]



          <?php
          $document = $modx->getObject('modresource', $id);
          
          if($document){
              return $document->hasChildren()? 'hasChildren' : 'noChildren';
          }
          
          return '';


          or, if you want to use isfolder instead:

          <?php
          $document = $modx->getObject('modresource', $id);
          
          if($document){
              return $document->get('isfolder')? 'hasChildren' : 'noChildren';
          }
          
          return '';


          warning - if someone uses this, yous should have "modResource" instead of "modresource" within the code samples above.

          other than that it works a treat.

          thanks guys.
            • 3749
            • 24,544 Posts
            Good catch (fixed above). Sorry for any confusion. tongue
              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
              • 51315
              • 67 Posts
              Quote from: BobRay at May 11, 2011, 11:42 PM
              The code above seems like overkill to me (and it makes me nervous to use PHP reserved words as variable names):

              [[!HasChildren? &id = `23`]]



              <!--?php
              $document = $modx--->getObject('modResource', $id);
              
              if($document){
                  return $document->hasChildren()? 'hasChildren' : 'noChildren';
              }
              
              return '';


              or, if you want to use isfolder instead:

              <!--?php
              $document = $modx--->getObject('modResource', $id);
              
              if($document){
                  return $document->get('isfolder')? 'hasChildren' : 'noChildren';
              }
              
              return '';

              Dear Bob,
              I know it's an old post, but what if we want to find out if a parent's resource has children, but also if that same resource has a published status?
              Example: I need to generate the class "hasChildren" only if a parent has a child who is also published.
                • 4172
                • 5,888 Posts
                you can try this:

                       $document = $modx->getObject('modResource', $id);
                        
                       if($document){
                           $c = $modx->newQuery('modResource');
                           $c->where(array(
                               'parent' => $document->get('id'),
                               'published' => '1',
                               'deleted' => '0'
                           ));
                           $has_children = $modx->getCount('modResource',$c);           
                           return $has_children ? 'hasChildren' : 'noChildren';
                       }
                
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 51315
                  • 67 Posts
                  Thank you Bruno, works like a charm!
                    • 51315
                    • 67 Posts
                    Quote from: Bruno17 at Nov 07, 2017, 08:40 PM
                    you can try this:

                           $document = $modx->getObject('modResource', $id);
                            
                           if($document){
                               $c = $modx->newQuery('modResource');
                               $c->where(array(
                                   'parent' => $document->get('id'),
                                   'published' => '1',
                                   'deleted' => '0'
                               ));
                               $has_children = $modx->getCount('modResource',$c);           
                               return $has_children ? 'hasChildren' : 'noChildren';
                           }
                    

                    Actually, this snippet suggests many situations.
                    For instance, it works perfectly with pdoMenu to hide a parent resource if no published children are found.

                    hasChildren = show = display: none
                    noChildren = hide = display: block
                    &tpl=`@INLINE <span class="[[!hasChildren?&id=`[[+id]]`]]"><a href="[[+link]]">[[+menutitle:]]</a>[[+wrapper]]</span>`