We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25979
    • 178 Posts
    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?//
      • 16337
      • 44 Posts
      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...
        • 5340
        • 1,624 Posts
        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
          • 5340
          • 1,624 Posts
          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
            • 22427
            • 793 Posts
            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)!
              • 5340
              • 1,624 Posts
              ... 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
                • 3749
                • 24,544 Posts
                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
                  • 16337
                  • 44 Posts
                  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!
                    • 5340
                    • 1,624 Posts
                    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
                      • 3749
                      • 24,544 Posts
                      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