We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48586
    • 78 Posts
    Hello, I was wondering if there was a way to return part of my current URL. I looked into the $modx->makeurl();, but can't figure out how to get it to return correctly. Here is an example of what I would like to happen:

    Let's say I am creating a travel agency website, and a user is at the following URL:

    http://www.mysite.com/miami-beach/package-deals/hotels

    I have a drop-down menu where they can change their location, but I would like to keep them on the same page displaying the updated city information, so after clicking the drop-down the URL would look like:

    http://www.mysite.com/key-west/package-deals/hotels

    I have a function on my current Joomla site that looks like:

    function getLocationUrl($market) {
    			$route = '/' . JFactory::getApplication()->getMenu()->getActive()->route;
    			$url = '?mc=' . $market;
    
    			return $route === '/home' ? $url : $route . $url;
    		}
    		$loc_url = getLocationUrl($market_variable_name);


    A sample item in my drop-down menu looks like:

    <li class="first hoverFix"><a href="/key-west<?= $loc_url ?>-key-west">Key West</a></li>


    (The current last part of the code is for tracking purposes, so the URL on initial click looks like http://www.mysite.com/miami-beach/package-deals/hotels?mc=key-west-miami-beach)

    Is there something similar in MODX that would return something like "JFactory::getApplication()->getMenu()->getActive()->route;"

    Thank you very much.

    This question has been answered by rjohnson375. See the first response.

      • 5430
      • 247 Posts
      There's a really good chance I'm not totally grasping what you're after here, but if your URL path is a reflection of your resource tree structure you could use something like setPlaceholders (https://github.com/oo12/setPlaceholders) to detect the parentage of the current location and dump those values into placeholders (using Uparent[level]). You could use those placeholders to populate your dropdowns.

      You probably already know this, but you can create URLs with parameters using syntax such as:

      [[~[[*id]]? &parameter1=`value` &parameter2=`value` etc...]]


      Using a setPlaceholders call somewhere in your template or resource that gathers your ancestors into placeholders you could very easily dump parameter values into the makeURL call using placeholders.
        • 48586
        • 78 Posts
        I will take a deeper look into that, I'm still very new with MODX, and in the past month I've learned a lot and looking back I'm sure I could redo the site a lot cleaner, but I have a tight deadline coming up. Currently my resource tree looks something like:

        - Tampa
        -- Hotels
        -- Restaurants
        -- Spas
        -- Activities
        - Key West
        -- Hotels
        -- Restaurants
        -- Spas
        -- Activities
        - Miami Beach
        -- Hotels
        -- Restaurants
        -- Spas
        -- Activities


        This was to keep the market name as part of the URL, and there may be better ways to do it, but I am learning MODX on the fly.

        I want to be able to dynamically change markets from any of the landing pages, and still retain the URL path. My drop-down only has the market names:

        - Tampa
        - Key West
        - Miami Beach
          • 30585
          • 833 Posts
          Assuming I understood your question, you can use UltimateParent to recreate your URL structure. According to its description, UltimateParent travels up the document tree to return the "ultimate" parent of a document.

          [[UltimateParent? &id=`id_of_the_location_hotels_page` &topLevel=`2`]] => Will return the ultimate parent (ID) of the specified document at a depth of 2 levels in the document hierarchy, with the root level being level 1. With that ID known, you can easily rebuild your URL structure.

          For example, if you were linking to the Key West resource,
          - Tampa (5)
            -- Hotels (6)
            -- Restaurants (7)
            -- Spas (8)
            -- Activities (9)
          - Key West (10)
            -- Hotels (11)
            -- Restaurants (12)
            -- Spas (13)
            -- Activities (14)
          - Miami Beach (15)
            -- Hotels (16)
            -- Restaurants (17)
            -- Spas (18)
            -- Activities (19)
          

          Your Key West dropdown menu item could look like this:
          <li class="first hoverFix"><a href="[[~[[UltimateParent? &id=`11` &topLevel=`2`]]]]">Key West</a></li>
          // This part: [[UltimateParent? &id=`11` &topLevel=`2`]] will be replaced with 10. So your URL will become this: [[~10]]
          // The rendered URL should be : key-west/ 
          // To get an absolute URL, just add the full scheme to your link tag: [[~[[UltimateParent? &id=`11` &topLevel=`2`]]? &scheme=`full`]]. This should output: http://www.mysite.com/key-west/
          // To get http://www.mysite.com/key-west/package-deals/hotels?mc=key-west-miami-beach
          // Just add package-deals/hotels?mc=key-west-miami-beach after your link tag
          // <a href="[[~[[UltimateParent? &id=`11` &topLevel=`2`]]? &scheme=`full`]]package-deals/hotels?mc=key-west-miami-beach">Key West</a>
          

          Put together, you dropdown menu should now look like this:
          <ul class="dropdown">
            <li class="first HoverFix"><a href="[[~[[UltimateParent? &id=`11` &topLevel=`2`]]? &scheme=`full`]]package-deals/hotels?mc=key-west-miami-beach">Key West</a></li>
            <li class="HoverFix"><a href="[[~[[UltimateParent? &id=`16` &topLevel=`2`]]? &scheme=`full`]]package-deals/hotels?mc=key-west-miami-beach">Miami Beach</a></li>
            <li class="HoverFix"><a href="[[~[[UltimateParent? &id=`6` &topLevel=`2`]]? &scheme=`full`]]package-deals/hotels?mc=key-west-miami-beach">Tampa</a></li>
          </ul>
          


          I didn't test this, but it should work. I'm sure there are better ways, but this is fairly easy. Hope it helps. [ed. note: treigh last edited this post 11 years, 6 months ago.]
            A MODx Fanatic
            • 48586
            • 78 Posts
            Thank you claytonk and treigh, I feel like these solutions are very close, but my site is of course a little more complex than my basic example.

            EDIT: I think I'm pretty close to a solution creating a custom snippet with a REQUEST_URI and using str_replace. [ed. note: rjohnson375 last edited this post 11 years, 6 months ago.]
            • discuss.answer
              • 48586
              • 78 Posts
              I know this was a very random question (most of the questions I post on this forum are very random, but the community has been fantastic in inspiring me to find a solution), just in case anyone is doing something similar on their website and scratching their head here is the solution I came up with. Basically I created a chunk with my drop-down locations and added a snippet to the end of the URL.

              Chunk code for drop-down

              <ul>
                <li><a href="tampa/[[switchLanding]]" onclick="updateCookie('tampa');">Tampa</a></li>
                <li><a href="key-west/[[switchLanding]]" onclick="updateCookie('key-west');">Key West</a></li>
                // etc
              </ul>


              My site relies on cookies to determine the users location, and they can change the cookie from the onclick function.

              switchLanding snippet

              <?php
              switch ($_COOKIE['market']) {
              
                case "tampa":
                  $url = $_SERVER['REQUEST_URI'];
                  switch (true) {
                    case strstr($url, 'tampa'):
                      $currenturl = str_replace('/tampa/','',$url);
                      return $currenturl;
                      break;
                  }
                  break;
              
                case "key-west":
                  $url = $_SERVER['REQUEST_URI'];
                  switch (true) {
                    case strstr($url, 'key-west'):
                      $currenturl = str_replace('/key-west/','',$url);
                      return $currenturl;
                      break;
                  }
                  break;
              
                // etc
              
              }
              [ed. note: rjohnson375 last edited this post 11 years, 6 months ago.]