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 2.1.0, I will upgrade soon and let you know, Thanks!
      • 8489
      • 90 Posts
      Just updated to 2.1.2-pl still getting the error on the alias field.
        • 4172
        • 5,888 Posts
        can you show your furl-system-settings again?
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 8489
          • 90 Posts
          Attached Furl settings

          When I want to insert a new product I duplicate an existing product so it saves me filling in a view TV’s.
          Could "duplicating" be the problem?
            • 4172
            • 5,888 Posts
            strange, I get this error
            Please correct the errors in your form before submitting
            only when I set automatic-alias system-setting to ’No’ and try to save a resource with an emtpy alias

            also when I first duplicate a resource and then go to edit this resource (it has the same alias at this time as the original resource), my plugin creates a new alias with ’pagetitle-newnumber’ after saving.

            did you check onBeforeDocFormSave for this plugin?
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 8489
              • 90 Posts
              Yes I use onBeforeDocFormSave.

              When I duplicate a document and using the same name as the original the alias field is blank, when I try to save I get the error it seems like no automatic alias is set.

              When I fillin by hand the alias field with the same productname as the original and try to save I get the error also.
                • 52884
                • 25 Posts
                Just in case someone is looking for a solution. I was able to use Brunos Plugin in Revo 2.5 with some minor adjustments. I defined a default value "alias" for the alias field via form customization and hide this field for creating new resources. This way, there are no longer error messages regarding existing or empty alias.

                Attached the code I use for plugin running on event OnBeforeDocFormSave.


                
                // specify allowed contexts
                $allowed_ctx = array('MYCTX');
                
                // make sure we're using the right event
                if (!in_array($modx->event->name, array('OnBeforeDocFormSave'))) return;
                if (!$resource) return;
                
                // make sure this only runs on new resources in specified contexts
                if ($mode != 'new') return;
                $ctx_key = $resource->get('context_key');
                if (!in_array($ctx_key, $allowed_ctx)) return;
                
                 
                
                // $alias = $resource->get('alias'); // don't needed
                // $id = $resource->get('id'); // $id is always 0 for new resources so we don't use that.
                $pagetitle = $resource->get('pagetitle');
                
                // create alias for lookup from pagetitle
                $alias = $resource->cleanAlias($pagetitle);
                
                // look for resources in ctx with existing alias but only if id != 0 (new resource)
                $c = $modx->newQuery('modResource', array('alias' => $alias, 'id:!=' => 0, 'context_key' => $ctx_key));
                if ($collection = $modx->getCollection('modResource', $c)) {
                    foreach ($collection as $key => $object) {
                        $existing[] = $object->get('alias');
                    }
                 
                    $c = $modx->newQuery('modResource', array('pagetitle' => $pagetitle, 'id:!=' => 0, 'context_key' => $ctx_key));
                    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;
                        }
                        //$suffix = "-1";
                 
                        $newalias = $pagetitle . $suffix;
                        $newalias = $resource->cleanAlias($newalias);
                        $loop = in_array($newalias, $existing);
                        $i++;
                    }
                 
                    $resource->set('alias', $newalias);
                }
                
                // if no conflicting resources found, set alias we created from pagetitle
                else {
                    $resource->set('alias', $alias);
                }
                
                
                return null;
                  • 13428 ☆ A M B ☆
                  • 1,031 Posts
                  Just one notice: It should be enough to restrict the search for the same alias to resources with the same parent. And getCollection should be replaced by getIterator.
                    • 3749
                    • 24,544 Posts
                    I would add this at the top:

                    $existing = array();


                    Also, maybe I'm misreading your code, but it looks like your getCollection() call won't get resources with that alias that already have a suffix, so you could be creating duplicate aliases with the same suffix.

                    This line:

                    $loop = in_array($newalias, $existing);


                    might work better as something like:

                    $exists = (bool) $modx->getCount('modResource', array('alias' => $newalias));
                    $loop = in_array($newalias, $existing) || $exists; 


                      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