<![CDATA[ how to find if resource has children? - MODX Community Forums]]> https://forums.modx.com/thread/?thread=29913 <![CDATA[Re: how to find if resource has children?]]> https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children?page=2#dis-post-555003 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>`
]]>
fredela Nov 07, 2017, 11:26 PM https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children?page=2#dis-post-555003
<![CDATA[Re: how to find if resource has children?]]> https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children?page=2#dis-post-554999 fredela Nov 07, 2017, 10:10 PM https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children?page=2#dis-post-554999 <![CDATA[Re: how to find if resource has children?]]> https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children?page=2#dis-post-554997
       $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';
       }
]]>
Bruno17 Nov 07, 2017, 08:40 PM https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children?page=2#dis-post-554997
<![CDATA[Re: how to find if resource has children?]]> https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children?page=2#dis-post-554996 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.
]]>
fredela Nov 07, 2017, 04:12 PM https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children?page=2#dis-post-554996
<![CDATA[Re: how to find if resource has children?]]> https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children?page=2#dis-post-162213 ]]> BobRay May 21, 2011, 01:32 AM https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children?page=2#dis-post-162213 <![CDATA[Re: how to find if resource has children?]]> https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children?page=2#dis-post-162212 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.]]>
krisj May 20, 2011, 12:19 PM https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children?page=2#dis-post-162212
<![CDATA[Re: how to find if resource has children?]]> https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children?page=2#dis-post-162211 thanks!]]> krisj May 12, 2011, 11:08 AM https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children?page=2#dis-post-162211 <![CDATA[Re: how to find if resource has children?]]> https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children?page=2#dis-post-162210

]]>
cipa May 12, 2011, 08:59 AM https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children?page=2#dis-post-162210
<![CDATA[Re: how to find if resource has children?]]> https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children?page=2#dis-post-162209
[[!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 '';
]]>
BobRay May 11, 2011, 11:42 PM https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children?page=2#dis-post-162209
<![CDATA[Re: how to find if resource has children?]]> https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children#dis-post-162208 So it seems isFolder controls the FURL. On the default FURL settings isFolder will add .html if uncheck even if it has children.
Yes, I think that’s the case (it’s isfolder, BTW, not isFolder -- all resource fields are lowercase). I think isfolder also controls the treatment in Wayfinder and getResources.

hasChildren() just checks to see if there are children. isfolder is used because it’s much faster to just check the resource field than to do a query to find out if there are children.]]>
BobRay May 11, 2011, 11:30 PM https://forums.modx.com/thread/29913/how-to-find-if-resource-has-children#dis-post-162208