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

    I'm looking for a way to insert a template variable into the URL Alias field.

    Right now if I insert [*exampletv1*] into the Friendly URLs field it will change to exampletv1, obviously this field does not allow for characters like [ or *.

    Is there a way to have the Friendly URL field process this request?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      No, since those are illegal characters in a URL they get stripped out. You would need to use a plugin get the TV value and replace the alias field with that value.

      Out of curiosity, why would you want to have a TV set the alias value? The alias is set per resource anyway.
        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
        • 10208 ☆ A M B ☆
        • 1,780 Posts
        I was wondering the same thing, it seems redundant. The field works just fine as is. huh
          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
          • 41020
          • 3 Posts
          Hey Guys - thanks for your responses.

          I'm using the Template Variable to pull in the name of a city. I have about 10 Template Variables that I use throughout the site. I want the url of certain pages to be named after the city. So for Example

          if a Template Variable is
          [[townName1]]
          and
           [[townName1]]
          = Seattle WA I'd like the URL Alias field to hold : Doing business in
           [[townName1]]
          and obviously it would reflect "doing-business-in-seattle-wa"

          right now it reflects "doing-business-in-towname1"

          Now yes, it can be done manually... but if I wanted to scale this approach is there a way to make it dynamic? [ed. note: freefallin11 last edited this post 14 years, 1 month ago.]
            • 30023
            • 172 Posts
            I'd be inclined to write a plugin that writes the alias on the event OnDocFormSave.

            Something like (untested):

            $townname = $modx->getTemplateVar(array('townname1'));
            
            if ($townname['value']) { // Only do this for docs with a non-empty town name 
                $alias = 'doing-business-in-'.preg_replace('/[^a-z]+/i', '-', strtolower($townname['value']));
                $modx->db->update('alias', $modx->getFullTablename('site_content'), 'id='.$id);
            }
            


            -- Tim.

              • 3749
              • 24,544 Posts
              The problem is that when a user saves that URL and tries to return later, there won't be anything there because MODX has never heard of that URI unless you create a rewrite rule that converts the city to a query parameter, and then convert it back with a plugin like the one above when you display the page.


              ------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using.
              MODX info for everyone: http://bobsguides.com/modx.html
                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
                • 30023
                • 172 Posts
                Quote from: BobRay at Sep 01, 2012, 11:31 PM
                The problem is that when a user saves that URL and tries to return later, there won't be anything there because MODX has never heard of that URI unless you create a rewrite rule that converts the city to a query parameter, and then convert it back with a plugin like the one above when you display the page.

                Confused. If he uses a plugin like I described above it will be in the alias as required, MODx will know about it and there is no need for any rewrite rules or further plugins. All the above plugin does is 'override' the core's alias creation.

                I've made plugins write aliases before and I can't remember having any issues.
                  • 3749
                  • 24,544 Posts
                  Sorry, I misunderstood your plugin. I thought it was connected to OnWebPagePrerender (though it obviously isn't) and rewrote the URL on the fly.


                  ------------------------------------------------------------------------------------------
                  PLEASE, PLEASE specify the version of MODX you are using.
                  MODX info for everyone: http://bobsguides.com/modx.html
                    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