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

    Anyone ever experienced some weird execution order stuff when using "Ulimate Parent" from within a snippet?

    Here is my problem.

    I use UP in a link generation snippet to determine the ID of the parent page. In other words, I might have a bunch of children to a document, but on all pages on site I need a dynamically generated link (google tracking) to take structural traits ONLY form the parent page’s title. So, if you clicked a link on the home page it would be like href="outbound/mysitetitle/elementclickedfrom" I do NOT want it to become href="outbound/mysiteCHILDtitle/elementclickedfrom" OR href="outbound/mysiteCHILDofCHILDtitle/elementclickedfrom", so I am using Ultimate Parent.

    So, in the link gen script, I grab the parent document’s ID via Ultimate Parent. I then debug and print that retrieved ID to the page. All good.

    The next job in the script is to take that ID and use it to run down using getPageInfo and grab the "pagetitle"

    Now, this is where we have the first break. If i use the variable I set from UP, this returns nothing. If i hard code that ID, all is good.

    Right next to that UP varable print, I var_dump that array and the info is not of the ultimate parent, but of the actual page i’m on.

    Also what is totally weird is when I print these items, the var_dump comes first on the rendered page, even if it’s last in the execution order.

    Sound weird?

    Thanks for any help you can offer.

    -Rob
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Using var_dump or print_r will have output in unexpected locations. They are for debugging purposes, and PHP gets the output out immediately.

      As far as the rest of it goes, I’m a bit confused, but I think you’re going about this in the wrong way.

      In your snippet, you need to get the ultimate parent’s pagetitle. There are two ways you could go about this; you could use a parameter using the UltimateParent snippet as the parameter value:
      [!mySnippet? &up=`[[UltimateParent]]`!]

      Notice the order of the snippet tags; the outer snippet is uncached and the inner snippet is cached. It has to be this way, otherwise the value of UltimateParent won’t be evaluated first and your snippet will be trying to use the UP snippet tags as the value. This way the inner snippet is evaluated before the outer snippet is, so its return value is now in place. This will give you the $up variable in your snippet to work with.

      The other way would be to use
      $up = $modx->runSnippet('UltimateParent');
      inside your snippet code (or just extract the UltimateParent code into your snippet; it’s not all that much).
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 3749
        • 24,544 Posts
        It sounds like a cache problem. It happens often in Evolution when you have things nested. Try having the page cached and swapping the snipppet caching:

        [[mySnippet? &up=`[!UltimateParent!]`]]
          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
          • 25218
          • 7 Posts
          Thank you SOTTWELL and BOBRAY.

          I love your answers!

          I cannot explain the resolution to this, but a guy here turned on the friendly URLs, I sorted out few pathing issues in some snippets and it somehow works now??

          I will put your suggestions into practice going forward.

          Thanks,
          Rob
            • 3749
            • 24,544 Posts
            I’m glad you got it working. It may have been another kind of caching problem (e.g. you fixed it yourself, but the fix didn’t apply until the cache was cleared later), or it could just be that the "page not found" page is set to your home page and you ended up there because of a bad path, rather than the failure of your snippet.
              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