We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33982 ☆ A M B ☆
    • 476 Posts
    In MODx Evo, I would use the "GetField" snippet for a lot of things (http://modx.com/extras/package/getfield). I primarily used it to go up the tree a certain number of levels from the current resource. I often did this in conjunction with PHx to determine if the current resource was a child, grandchild, or great grand child of a specific Resource. For example, I often had this in Evo to determine the depth of the current resource:

    [+phx:if=`[[GetField? &field=`id` &parent=`1` &parentLevel=`2`]]`:is=`59`:then=`{{store_level2}}`:else=`{{store_level3}}`+]


    I see in Revo there is the "getResourceField" snippet, but I'm not seeing how to determine the grandparent or great grandparent of a current Resource. My attempt is to do something like so:

    -- Great Grandparent (1)
    -- Grandparent (5)
    --- Parent (45)
    ----- Child (77)

    My goal would be to determine if the current resource is the grandchild of Resource 1, then output "X", but if the current Resource is a great grandchild of Resource 1, then output "Y". I'd be using MODx Revo "If" snippet of course. [ed. note: flinx777 last edited this post 15 years, 1 month ago.]
      Precision Web Development ... SmashStack.com
      • 33976 ☆ A M B ☆
      • 571 Posts
      Hi flinx,

      You might want to have a look at the ultimateParent snippet (or over Github), which might achieve what you are looking for.
        • 33982 ☆ A M B ☆
        • 476 Posts
        Hey Romain,

        Thanks for your post.

        I have been using the UltimateParent snippet for Revo, but it only tells you the top Resources from the root down. So for example, it will tell you the ultimate parent of the current resource at a level of "x". I need to be able to determine the ID of the resource a certain number of levels up from the current Resource. Seems the UltimateParent works the other way around (unless I'm missing something).
          Precision Web Development ... SmashStack.com
          • 33976 ☆ A M B ☆
          • 571 Posts
          Sorry, i was too hasty (also, i'm not using ultimateparent often).

          Anyway, this post/snippet might be of any use.
            • 3749
            • 24,544 Posts
            I can't think of any elegant way to do that in Revo (which doesn't mean there isn't one). The only thing that comes to mind is a set of pretty inelegant snippets:

            <?php 
            /* GetGrandParent */
            
            $parent = $modx->resource->getOne('Parent');
            $grandparent = $modx->resource->GetOne('Parent');
            if ($grandparent) {
                return $grandparent->get('id');
            } else {
                return "";
            }


            <?php
            /* get GreatGrandParent */
            $parent = $modx->resource->getOne('Parent');
            $grandparent = $modx->resource->getOne('Parent');
            $greatgrandparent = $grandparent->getOne('Parent');
            if ($greatgrandparent) {
                return $greatgrandparent->get('id');
            } else {
                return "";
            }
            


            A single snippet would be much more efficient; something like this:

            [[!ShowStuff]]

            <?
            /* ShowStuff snippet */
            
            $grandparentId = null;
            $greatgrandparentId = null;
            
            $parent = $modx->resource->getOne('Parent');
            $grandparent = $modx->resource->getOne('Parent');
            $greatgrandparent = $grandparent->getOne('Parent');
            if ($grandparent) {
                $grandparentId = $grandparent->get('id');
            }
            
            if ($greatgrandparent) {
                $greatgrandparentID = $greatgrandparent->get('id');
            }
            
            if ($grandparentId == 1 {
                return 'x';
            } else if ($greatgrandparentId == 1) {
                return 'y';
            }
            return "";
            





            [ed. note: BobRay last edited this post 15 years, 1 month ago.]
              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
              • 33982 ☆ A M B ☆
              • 476 Posts
              Hey BobRay,

              Thanks for the response ... will give it a spin. My hope was to find a snippet a little more flexible where a parameter could be specified to go up the tree a certain number of levels. The MODx Evo GetField snippet handled that easily with the "&parentLevel" parameter. Made it really easy for me in Evo to setup the tree structure in the backend so that clients didn't have to change templates or T.V.'s for certain levels in the tree to output certain content on the front of the site, but rather the snippet would output different content based on the level in the tree. But this may be a good starting point in the meantime.

              Thanks again for posting that up!
                Precision Web Development ... SmashStack.com
                • 4172
                • 5,888 Posts
                you can also try this simple snippet (untested):

                <?php
                //[[getParentId? &level=`3` ]]
                //[[getParentId? &level=`2` &direction=`down`]]
                
                $parentIds = $modx->getParentIds($modx->resource->get('id'),99);
                
                return $direction=='down' ? $parentIds[count($parentIds)-$level-1] : $parentIds[$level-1];
                
                
                ?>
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 3749
                  • 24,544 Posts
                  I forgot about getParentIDs. That will be *much* more efficient than the method I posted.

                  I would be inclined to do it this way (untested):

                  <?php
                  //[[getParentId? &level=`3` ]] (greatgrandparent)
                  //[[getParentId? &level=`2`]]  (grandparent)
                  //[[getParentId? &level=`1`]]  (parent)
                   
                  $parentIds = $modx->getParentIds($modx->resource->get('id'),99);
                  return $parentIds[$level-1];


                  I'm 99% sure that the array starts with the parent and goes up from there. If not, add this before the return statement:
                  $parentIds = array_reverse($parentIds);

                  I think the first item (the parent) is $parentIds[0]. If not you can take out the -1. [ed. note: BobRay last edited this post 15 years, 1 month ago.]
                    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
                    • 33982 ☆ A M B ☆
                    • 476 Posts
                    @Bob ... thanks so much ... will give this a try today and report back on it. Thanks!
                      Precision Web Development ... SmashStack.com
                      • 33982 ☆ A M B ☆
                      • 476 Posts
                      @Bob ... finally implemented it today...works like a charm ... thanks so much, exactly what I needed!
                        Precision Web Development ... SmashStack.com