We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 43374
    • 39 Posts
    How can I reference the parent ID upwards multiple levels?

    I want to call a snippet in the documents (5)+(6) that references TVs of the ID (1) and call the same snippet in (15)+(16) that references TVs of the ID (10)

    My tree structure of the documents is:
    >web 
    
    >>US (1)
    >>>HOME (2)
    >>>>FAQ (4)
    >>>STORIES (3)
    >>>>NEWS (5)
    >>>>>LATEST NEWS (6)
    
    >>DE (10)
    >>>HOME (12)
    >>>>FAQ (14)
    >>>STORIES (13)
    >>>>NEWS (15)
    >>>>>LATEST NEWS (16)


    I only figured out who to achieve this with defining the ID but not how I can tell the snippet to use the highest parent of the document:

    <?php
    $page = $modx->getObject('modResource', 1);
    echo $page->getTVValue('tv_example_color');
    

    This question has been answered by multiple community members. See the first response.

      • 10208 ☆ A M B ☆
      • 1,780 Posts
      Have you looked at the getResources add on? If I'm not misunderstanding, you should be able to do what you want with getResources easily. http://rtfm.modx.com/display/ADDON/getResources

      Additionally, you'll want to use return instead of echo in your custom snippets within MODX.
        Frogabog- MODX Websites in Portland Oregon
        "Do yourself a favor and get a copy of "MODX - The Official Guide" by Bob Ray. Read it.
        Having server issues? These guys have MODX Hosting perfected - SkyToaster
        • 43374
        • 39 Posts
        As far as I understood getRecources I can only take the direct parent of a document but not the parent of the parent. I think depth works only downwards not upwards.
        • discuss.answer
          • 36926
          • 701 Posts
          Quote from: sh0ck23 at Jun 11, 2013, 12:49 PM

          I only figured out who to achieve this with defining the ID but not how I can tell the snippet to use the highest parent of the document:


          Have you had a look at the ultimateparent snippet?
          http://modx.com/extras/package/ultimateparent

          You can specify how many levels you wish to travel etc.

          • discuss.answer
            • 4172
            • 5,888 Posts
            If you want to get single fields use getResourceField together with UltimateParent

            http://modx.com/extras/package/ultimateparent
            http://modx.com/extras/package/getresourcefield

            If you want output more than one field use getResources together with ultimateparent to get the Resource and its TVs all at once.

            you can also use the @INHERIT - Binding as the TV-default-value.
            This does inherit the value from parents to its children.
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
            • discuss.answer
              • 35150 ☆ A M B ☆
              • 191 Posts
              getResourceField together with Ultimate Parent would do that, or I have an extra which does both.

              You'd use it like this:
              [[setPlaceholders? &ph=`examplecolor == parent2.tv.tv_example_color`]]

              That gets the value of the TV tv_example_color from the current document's parent's parent and stores it in [[+examplecolor]].

              If you're writing your own snippet you can use getParentIds() to get an array of all of a resource's parents. Take a look here for an example.
                Extras :: pThumb • Resizer • imageSlim • setPlaceholders
                • 43374
                • 39 Posts
                Thanks guys

                The UltimateParent snippet works perfectly