On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Hi folks,

    I wondered if there is a way to simply check whether resource has children or not.

    Regards,

    Mike
      //Why use windows since there is a door?//
    • I would also like to find out how to do this.

      I am trying to add an extra class to html based on a condition if resource has children or has a parent other than `0` and can’t figure it out yet. Kind of have a workaround with *isfolder:eq=`1` filter but its not a great solution...

      please help

      many thanks...
      • There should be a function hasChildren();

        I think the proper way to use it is

        <php?
        return $modx->resource->hasChildren();


        So if you create a snippet "HasChildren" paste the code and call it [[HasChildren]] in your template it should return the number of children.

        PS: not tested - just trying to help smiley
        • A better version would be

          
          <?php
          
          $docId = isset($id) ? intval($id) : $modx->resource->get('id');
          $true = isset($true) ? $true : false;
          $false = isset($false) ? $false : false;
          
          $document = $modx->getObject('modResource', $docId);
          
          
          
          if($document){
              
              $hasChildren = $document->hasChildren();
              
              if($true && $false){
                  
                  if($hasChildren){
                      return $true;
                  }else{
                      return $false;
                  }
                  
              }
          
              return $hasChildren;
          
          }
          
          
          
          


          Call it [[HasChildren? &id=`23` &true = `hasChildren` &false = `noChildren` ]]


          This should work for generating clases
          • If a resource has children, automatically its variable [tt][[*isfolder]][/tt] gets the value 1. So if there aren’t any resources set as container despite not having children (which normally is the case) then you just need to refer to [tt][[*isfolder]][/tt].

            Note:
            I just found that if you put [tt][[*isfolder]][/tt] into the content of a page, then in case of a container you get the output 1 (like expected) - but if it is not a container, the output will be empty (instead of 0)!
            • ... but if you uncheck isfolder than the output will be empty even if it has children.

              Maybe a fix for the code above would be to test for unpublished documents.

              -------

              I think I found a bug in 2.1.0. rc3. Can some one confirm:

              If you have a container with children and you un-check ’Container’, save, check ’Container’ and preview the page.
              In my case I get the home page not the Container.

              I think there is some sort of redirect happening.
              Thanks
              • Quote from: cipa at May 11, 2011, 03:59 AM

                ... but if you uncheck isfolder than the output will be empty even if it has children.

                Maybe a fix for the code above would be to test for unpublished documents.

                -------

                I think I found a bug in 2.1.0. rc3. Can some one confirm:

                If you have a container with children and you un-check ’Container’, save, check ’Container’ and preview the page.
                In my case I get the home page not the Container.

                I think there is some sort of redirect happening.
                Thanks

                I think that’s because the version stored in the DB doesn’t match the version in memory (since you didn’t save it after checking the box). If containers have a different suffix, the URL won’t be correct, the page won’t be found, and you’ll be redirected to the error page (by default, the home page).

                FYI, isfolder is automatic in Evolution if the doc has children. In Revolution, OTOH, isfolder and $resource->hasChildren() (or $modx->resource-hasChildren() for the current doc) are independent.
                  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: cipa at May 11, 2011, 02:32 AM

                  A better version would be

                  
                  <?php
                  
                  $docId = isset($id) ? intval($id) : $modx->resource->get('id');
                  $true = isset($true) ? $true : false;
                  $false = isset($false) ? $false : false;
                  
                  $document = $modx->getObject('modResource', $docId);
                  
                  
                  
                  if($document){
                      
                      $hasChildren = $document->hasChildren();
                      
                      if($true && $false){
                          
                          if($hasChildren){
                              return $true;
                          }else{
                              return $false;
                          }
                          
                      }
                  
                      return $hasChildren;
                  
                  }
                  
                  
                  
                  


                  Call it [[HasChildren? &id=`23` &true = `hasChildren` &false = `noChildren` ]]


                  This should work for generating clases

                  for some reason this only works if the parent’s "isfolder" is set to false.

                  but it kind of works, thanks!
                  • Quote from: BobRay at May 11, 2011, 06:38 AM

                    I think that’s because the version stored in the DB doesn’t match the version in memory (since you didn’t save it after checking the box). If containers have a different suffix, the URL won’t be correct, the page won’t be found, and you’ll be redirected to the error page (by default, the home page).

                    FYI, isfolder is automatic in Evolution if the doc has children. In Revolution, OTOH, isfolder and $resource->hasChildren() (or $modx->resource-hasChildren() for the current doc) are independent.

                    So it seems isFolder controls the FURL. On the default FURL settings isFolder will add .html if uncheck even if it has children.


                    for some reason this only works if the parent’s "isfolder" is set to false.

                    I am not sure I get it. Maybe you can post your code in here
                    • 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.
                        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