We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36533
    • 65 Posts
    MODx Revolution 2.2

    I'm trying to adjust the URI for resources inside specific folders to include their parent path using a plugin. I have Use Friendly Alias Path set to off, as I want the rest of the web site to be accessible at the root level for all other pages.

    My structure:

    Topics (64)

    - - Category 1 (65)
    - - - Post 1 (xx)
    - - Category 2 (66)
    - - - Post 2 (xx)
    - - Category 3 (67)
    - - - Post 3 (xx)

    Assuming I had Alias Path set to on, I'd get /topics/cat-1, but instead I'm trying to override the URI to output /cat-1/topics, without paths on the rest of the web site. This seemed like an appropriate time to use a plugin.. so here is my attempt:

    <?php
    $parent= $resource->get('parent');
     
    if ($parent == '65') {
         $alias = $resource->get('alias');     
         $resource->set('uri_override', '1');     
         $resource->set('uri', $parent.'/topics/'.$alias);
         $resource->save();
    }
    


    OnBeforeDocFormSave is checked.


    This returns:

    www.domain.com/65/topics/post-1


    Close, but I obviously get the parent ID and not what I need, which is the alias of the parent. Also, it'd be better if I could just assign parent == '64', retrieve the alias of the categories (65,66,67) and set the uri for the resource's alias afterwards. This way I wouldn't have to do this for each category and change it manually as I add new categories.

    Any ideas?
    [ed. note: nuwebstudios last edited this post 14 years, 7 months ago.]
      • 3749
      • 24,544 Posts
      I think this will do it (untested):

      <?php
      
      $parentObj= $resource->getOne('Parent');
      
      /* the 'if' statement keeps it from crashing when run for docs at the root with no parent */
      if ($parentObj) {
          $parentId = $parent->get('id');
          $parentAlias = $parent->get('alias');
      }
        
      if ($parentId == '65') {
           $alias = $resource->get('alias');     
           $resource->set('uri_override', '1');     
           $resource->set('uri', $parentAlias.'/topics/'.$alias);
           $resource->save();
      }



      ---------------------------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
      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
        • 36533
        • 65 Posts
        Thanks for the reply BobRay.

        When I test this as is, I'm assume it is crashing, as it hangs on save.

        I changed:

        getOne('Parent');


        to:

        getOne('parent');


        I was thinking perhaps it was a case issue. It does allow the document to save after changing it, but it doesn't affect the URI. I've also attempted using it without your if statement, and it still crashes. Any ideas? [ed. note: nuwebstudios last edited this post 14 years, 7 months ago.]
          • 3749
          • 24,544 Posts
          No, it should definitely be 'Parent'. All getOne() and getMany() aliases start with an uppercase letter.

          Are you sure you're trying it on a a document whose parent has the ID 65? Otherwise, it will do nothing. That's what your code did, so I tried to duplicate it. Maybe that's not what you want.


          ---------------------------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
          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
            • 36533
            • 65 Posts
            Thanks for clarification on the Parent case. Yes, I tried it on a document under resource 65, but it hangs on save and never recovers.
              • 3749
              • 24,544 Posts
              I can't think why that would be unless the current user doesn't have the necessary permissions.

              Anything in the MODX error log?


              ---------------------------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
              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
                • 36533
                • 65 Posts
                Permission issues would be odd if one variant worked over the other, wouldn't it?

                I do have an error that I'll have to look into, but I don't think it is related:

                [2012-03-16 18:10:50] (ERROR @ /home/content/60/8873760/html/assets/components/smartoptimizer/connector.php : 124) PHP warning: filemtime() [<a href='function.filemtime'>function.filemtime</a>]: stat failed for index.php
                  • 36533
                  • 65 Posts
                  Is it possible this is related to a former bug that was resolved?

                  http://tracker.modx.com/issues/3057

                  Perhaps their fix didn't quite cover the bug in its entirety?
                    • 36533
                    • 65 Posts
                    I had 2 issues with your example BobRay, but it helped point me in the right direction. Thank you.

                    I pulled that code into a snippet and tried to echo the results and came out with two different errors:

                    Call to a member function getOne() on a non-object

                    Fixed by changing $parentObj to $parent

                    Call to a member function get() on a non-object

                    Fixed by changing defining explicitly what context I was working in:

                    $parent= $resource->getOne('Parent');
                     
                    /* the 'if' statement keeps it from crashing when run for docs at the root with no parent */
                    if ($parent) {
                        $parentId = $parent->get('id');
                        $parentAlias = $parent->get('alias');
                    }
                       
                    if ($parentId == '82') {
                         $resource->set('context_key', 'web');
                         $alias = $resource->get('alias');     
                         $resource->set('uri_override', '1');     
                         $resource->set('uri', $parentAlias.'/topics/'.$alias);
                         $resource->save();
                    }
                    


                    I'm not sure if this would cause any other conflicting issues or reveals issues with my security layer, but it works for me now. I effectively get the folder structure /topics/cat-1 to rewrite as /cat-1/topics.

                    A full explanation of my purpose for doing this is below, in case others find it interesting:

                    I'm working on setting up a blog, but I didn't want to do it the standard way of having a /blog sub-directory and store all my posts there. I have resources such as web-design, seo, graphic-design and want to experiment with running my blog posts through those containers. The problem is, it then becomes all decentralized and more difficult to manage.

                    This workaround above, with some help from my htaccess for redirecting purposes, allows me to store all my blog posts in my container folder /topics, while still maintaining my structure around my services. When I write about web design, instead of having www.domain.com/topics/web-design-post.. I can give my web design keyword a bit more weight having posts going through www.domain.com/web-design/topics/web-design-post or www.domain.com/web-design/web-design-post (which I'll likely switch it to instead).

                    It might not be the most elegant way to do this (for example tagging the posts would probably be better and then rewrite the URI), but it works for my purposes as I won't be changing categories often.