We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17723
    • 221 Posts
    Hello,
    I'm struggling how to auto generate an unique url for my resources (Revo 2.2.6).
    The editors of the page doesn't have access to edit manually alias, so when they want to save a resource that has the same page title as a previous resource that It won't save. Of course If they would have access to the alias field they would see that this field disables them to save a resource with the same alias.
    But in my case editors can't have access to the alias field.

    Is there a way to force modx to autogenerate alias based on pagetitle and Id? So it will be never a duplicate of an existing resource?
      Lucas from AroundCyprus.net
      • 3749
      • 24,544 Posts
      Not that I know of, but it's a great feature request.

      In the meantime, I think this plugin connected to OnDocFormSave might do it (untested).

      <?php
      $pageTitle = $resource->get('pagetitle');
      $code = $resource->get('id');
      // $code = date("Y.m.d-H:i:s");
      $alias = $resource->cleanAlias($pageTitle);
      $newAlias = $alias . $code;
      $resource->set('alias', $newAlias);
      $resource->save();


      Note that the ID will show up in the URL when the page is accessed and if you refactor existing resource to use this scheme, old links to them will be broken.

      You can prevent this from acting on existing resources by putting this at the top of the plugin, but then some URLs will have numbers in them and some won't.

      /* act only on new resources */
      if ($mode != modSystemEvent::MODE_NEW) return;


      BTW, some people solve this by appending some version of the date and/or time to the alias.

      OTOH, you might be better off just training the editors to change the page title when it won't save, since the alias is based on that. [ed. note: BobRay last edited this post 13 years, 6 months ago.]
        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
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        There really should be a pop-up error message for those. It can be quite annoying when "it won't save" because it's a required field or a duplicate value in a field that's off the screen!
          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