We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    Normally, the [[~123]] syntax for generating a resource’s URL is awesome, but I’m stuck in a situation where I actually need relative URLs, and I’m wondering if there’s any recommended way to do it.

    The situation is that we have a lot of copied folders which use the same structure throughout: each client gets their own folder of pages which is created by duplicating a "master_folder". However, any absolute links become a problem, e.g. a link to master_folder/child/page.html ( [[~123]]) remains unchanged in the copy, whereas it should reflect the corresponding child page in the client folder, e.g. client_folder/child/page.html ([[~456]]). The behavior when you duplicate a resource and its children is to leave the content untouched (so the links remain pointing to [[~123]] back in the master folder when I want them pointing to the child/page.html that is relative to the client’s folder. Make sense?

    One idea is this:
    1. Lock down the alias field on the pages so only a super user manager can edit them.
    2. Use the UltimateParent snippet to get the id of the parent folder (e.g. client_folder), then us a combination of numerical MODx links and hard-coded aliases in my hrefs, e.g.
    <a href="[[~555]]child/page.html">Child Page for this Client</a>


    That seems a little low-brow, and changing the aliases of any of the folders involved would break the links... but then again, I’m already breaking the links when I copy the master_folder anyway.

    Another idea would be an output filter, e.g.
    [[~123:make_url_relative_to=[[!UltimateParent]]]]


    Where UltimateParent retrieves the the id of the client folder homepage.

    Any other thoughts or perhaps existing solutions?
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      Ok, I figured out a solution to this using the existing stripString output filter.

      Imagine a structure like this:

      main_folder (123)
      +-- some_page.html (124)
      copy_folder (456)
      +-- some_page.html (457)

      So in the main folder, I wanted to make a link to some_page.html (124) that would survive being copied to the copy_folder so the link would survive as a relative link in the copy.

      [[~456]][[~124:stripString=`[[~123]]`]]
      // Or, using UltimateParent to determine which copied folder we're in
      [[~[[!UltimateParent]]]][[~124:stripString=`[[~123]]`]]
      


      Kinda ugly, but it seems to work.