We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36502 ☆ A M B ☆
    • 491 Posts
    Hi, I noticed that when a user duplicate a resource, if he doesn't change the title before saving, Modx assigns the same URI.

    This causes the error inside the log report:
    [2014-01-30 03:14:25] (ERROR @ /index.php) Resource URI path/2013/10/28/alias/ already exists for resource id = 104; skipping duplicate resource URI for resource id = 110

    The problem is that with sites with high traffic this cause the error.log grows very fast, as long as the error is repeated several times!

    How can I prevent this behaviour? Maybe a plugin that control before saving?
    thanks
      TilliLab | MODX Ambassador
      website
      • 4172
      • 5,888 Posts
      Hi tillilab,

      yes, I think a plugin, which runs on the event OnResourceDuplicate could check for duplicate URIs
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 3749
        • 24,544 Posts
        There are the allow_duplicate_alias and global_duplicate_uri_check System Settings. I'm not sure how they work when you duplicate a Resource, though.

        BTW, SiteCheck is handy to have when the duplicates occur because it will show you the sets of duplicates with links to edit either one. http://bobsguides.com/sitecheck-tutorial.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
          • 36502 ☆ A M B ☆
          • 491 Posts
          Thanks for your reply,
          I tried to create a plugin associated to the event OnResourceDuplicate that add the id of the resources before the alias:

          <?php
          $eventName = $modx->event->name;
          switch($eventName) {
            case 'OnResourceDuplicate':
                $alias = $newResource->get('id').'-'.$newResource->get('alias');
                $newResource->set('alias', $alias);
                $newResource->save();
          return;
          }


          usually it works...but I'm afraid that sometimes it doesn't... may be if the user uses the right click?
          thanks again

          @Bob great component, I'll give a try to SiteCheck!
            TilliLab | MODX Ambassador
            website
            • 3749
            • 24,544 Posts
            FYI, if it's only attached to the one event, you don't need the test, just this:

            $alias = $newResource->get('id').'-'.$newResource->get('alias');
            $newResource->set('alias', $alias);
            $newResource->save();


            This might make it more reliable (or not):

            $docId = $newResource->get('id');
            $modx->reloadContext('web'); // or whatever context it's in
            $obj = $modx->getObject($docId);
            $alias = $docId . '-' . $newResource->get('alias');
            $obj->set('alias', $alias);
            $obj->save();

              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