We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8489
    • 90 Posts
    I use the same name for multiple products, so when I create a product page with the same name I got an error because I used the name already.
    Is it possible to add an auto-increment integer at the end of an url?

    Something like:

    www.website.nl/product
    www.website.nl/product-01
    www.website.nl/product-02
    www.website.nl/product-03




      • 33968
      • 863 Posts
      Can’t remember but I thought this was default behaviour in Evo. Might be thinking of something else though.

      I couldn’t find a system setting for that in Revo. Maybe you need a plugin (OnBeforeDocFormSave?) to check if the alias exists and if so, append an auto-increment integer to the end...

      See core/model/modx/processors/resource/create.php for inspiration smiley
        • 8489
        • 90 Posts
        Thanks Lucas,

        Do you maybe know some example plugins that can do such action?
          • 4172
          • 5,888 Posts
          this needs some testing,
          but something like that as a plugin for onBeforeDocFormSave could do it:

          <?php
          
          // Get the current event
          $e = &$modx->event;
          $r = &$e->params['resource'];
          
          // set new alias
          $alias = $r->get('alias');
          $id = $r->get('id');
          $pagetitle = $r->get('pagetitle');
          
          
          //here you need to check for existing aliases and create a new incremented one
          //this needs some testing
          $c = $modx->newQuery('modResource', array('alias' => $alias, 'id:!=' => $id));
          if ($collection = $modx->getCollection('modResource', $c)) {
              foreach ($collection as $key => $object) {
                  $existing[] = $object->get('alias');
              }
          
              $c = $modx->newQuery('modResource', array('pagetitle' => $pagetitle, 'id:!=' => $id));
              if ($collection = $modx->getCollection('modResource', $c)) {
                  foreach ($collection as $key => $object) {
                      $existing[] = $object->get('alias');
                  }
              }
          
              $loop = true;
              $i = 0;
              while ($loop) {
                  if ($i == 0) {
                      $suffix = '';
                  } else {
                      $suffix = $i < 10 ? '-0' . $i : '-' . $i;
                  }
          
          
                  $newalias = $pagetitle . $suffix;
                  $newalias = $r->cleanAlias($newalias);
                  $loop = in_array($newalias, $existing);
                  $i++;
              }
          
              $r->set('alias', $newalias);
          }
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 8489
            • 90 Posts
            Thanks Bruno17 for your example will try it right now!
              • 4172
              • 5,888 Posts
              just edited this row:
              $newalias = $r->cleanAlias($newalias);
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 8489
                • 90 Posts
                Thanks Bruno17,

                I tried with the first code and one with the correction but it give the (Please correct the errors in your form before submitting) error on the alias field.


                  • 4172
                  • 5,888 Posts
                  set automatic-alias system-setting to yes and try again
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 8489
                    • 90 Posts
                    Thanks Bruno17, I tried that but still it give the error, attached my settings.
                      • 4172
                      • 5,888 Posts
                      which version of revo? I’m on 2.1.2 and seems to work.
                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!