On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • 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
    • I agree. Simple is better smiley

      • thats elegant BobRay wink thanks!
        • 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.
          • 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
            • 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.
              • 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!
                • Thank you Bruno, works like a charm!
                  • 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>`