We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45153
    • 6 Posts
    Hi All,

    Sorry if this has already been asked.

    Is there a way I can check if a resource is level 2 and has children and then print specific code in a template?

    I've seen:
    [[If?
    &subject=`[[*isfolder]]`
    &then=`Some html Here`
    &else=`Different html Here`
    ]]

    But I'm not sure how to check if it's only second level folder.

    Thanks in advance.
    • Sounds like a custom snippet you should write.. I cannot remember something that covers this
        MODX Ambassador (NL) & Professional MODX developer
        Follow me on Twitter | Visit my page on Facebook | View my code on Github | View my script posts
        MODX e-commerce solution SimpleCart
        • 3749
        • 24,544 Posts
        This snippet would return a number giving the depth and you could use that in your conditional. You'll have to experiment to see what number it returns for your 2nd-level resources.

        [[!GetLevel]]


        <?php
        /*GetLevel snippet */
        $parentIds = $modx->getParentIds($modx->resource->get('id'))
        return count($parentIds);
        


          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
          • 3749
          • 24,544 Posts
          Here's an enhancement that will also test isfolder at the same time:

          [[!GetLevel]]


          <?php
          /*GetLevel snippet */
          $parentIds = $modx->getParentIds($modx->resource->get('id'))
          $count = count($parentIds);
          return $modx->resource->get('isfolder')? $count : '';
          


          If it's not a folder, the snippet with return nothing. If it is, it will return the level.

          Note that unlike Evolution, a non-folder can have children and a folder can have no children.

          If you want to test whether it has children (rather than the isfolder setting), change the last line to:

          return $modx->resource->hasChildren()? $count : '';


          BTW, I would put the desired output in the snippet as well for speed or use $modx->getChunk() to pull it in from a chunk.
            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
            • 45153
            • 6 Posts
            Quote from: BobRay at Sep 18, 2013, 09:50 PM
            Here's an enhancement that will also test isfolder at the same time:

            [[!GetLevel]]


            <!--?php
            /*GetLevel snippet */
            $parentIds = $modx--->getParentIds($modx->resource->get('id'))
            $count = count($parentIds);
            return $modx->resource->get('isfolder')? $count : '';
            


            If it's not a folder, the snippet with return nothing. If it is, it will return the level.

            Note that unlike Evolution, a non-folder can have children and a folder can have no children.

            If you want to test whether it has children (rather than the isfolder setting), change the last line to:

            return $modx->resource->hasChildren()? $count : '';


            BTW, I would put the desired output in the snippet as well for speed or use $modx->getChunk() to pull it in from a chunk.

            Thank you sooo much!!!!
              • 3749
              • 24,544 Posts
              Don't thank me until it works. wink
                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've tried to use this snippet with revo 2.4.2 but it does not seems to work anymore.
                What to do to make it work again ?
                  • 3749
                  • 24,544 Posts
                  If you're using the version from the post above, remove this line:

                  <!--?php


                  It shouldn't be there.
                    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
                  • Yes i've seen that but i use this code :

                    <?php
                    /*GetLevel snippet */
                    $parentIds = $modx->getParentIds($modx->resource->get('id'))
                    return count($parentIds);


                    But it is not working.
                    • It was just missing a semicolon :

                      /*GetLevel snippet */
                      $parentIds = $modx->getParentIds($modx->resource->get('id'));
                      return count($parentIds);