We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48853
    • 12 Posts
    Hi to everyone!

    I setup FURL Alias Maximum Length e.g. to 50 chars and i think it'll be logically to automatically trim rest of generated string to value of Alias Maximum Length. Question is how?

    I suppose solution is to write a snippet which trim a length of Resource Alias but im not familiar with Modx API and this task is tricky for me.
    It would be nice to findout elegant solution.

    Will be very appreciate for any help.
    • You would most likely need a plugin attached to the OnBeforeDocFormSave event to process the alias value prior to saving to the database.

      Creating a plugin in:
      http://rtfm.modx.com/revolution/2.x/developing-in-modx/basic-development/plugins

      The OnBeforeDocFormSave event:
      http://rtfm.modx.com/revolution/2.x/developing-in-modx/basic-development/plugins/system-events/onbeforedocformsave

      Should be fairly straightforward to get the alias. How you trim it through php will be where the time will be spent (remember that you will need it to be unique)
        • 3749
        • 24,544 Posts
        There is also the friendly_alias_max_length System Setting. It should trim them automatically when you set the automatic_alias to true and tell users not to fill in the alias field (or hide it with Form Customization).


        If you need to do it in code, I think if you create the resource then do this, it will respect that max setting:

        $alias = $resource->cleanAlias($pagetitle);


        or

        $alias = $resource->cleanAlias($resource->get('pagetitle'));
        
          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
          • 48853
          • 12 Posts
          Thank you all for your help!

          Thank BobRay personally for useful tip:
          $resource->cleanAlias($resource->get('pagetitle'));


          Everything is really simple ))

          Here is my first Modx module:

          System event: OnBeforeDocFormSave
          <?php
          
          $alias = $resource->cleanAlias($resource->get('pagetitle'));
          $alias = mb_substr($alias, 0, 50); // To Do: get value from friendly_alias_max_length 
          $modx->event->output($alias);


          ...And couple more questions:
          How i can get value from friendly_alias_max_length?

          This solution works only for new documents, but changes nothing in already generated aliases. [ed. note: denis_ last edited this post 9 years, 5 months ago.]
          • Have you tried using $resource->set('alias', $alias); in the code? This would seem to save the new value to the $resource object's alias property prior to saving to the database.

            I am doing something similar (where I instead add longtitle and pagetitle together to get an alias) and it works on both New and Update documents.

            you should be able to get the friendly_alias_max_length value by using $modx->getOption('friendly_alias_max_length')

              • 48853
              • 12 Posts
              Quote from: dvstudiosinc at Nov 14, 2014, 03:28 PM
              Have you tried using $resource->set('alias', $alias); in the code? This would seem to save the new value to the $resource object's alias property prior to saving to the database.

              I am doing something similar (where I instead add longtitle and pagetitle together to get an alias) and it works on both New and Update documents.

              you should be able to get the friendly_alias_max_length value by using $modx->getOption('friendly_alias_max_length')

              Thank you for answer ))

              Here is that what i have:
              <?php
              
              $alias = $resource->cleanAlias($resource->get('pagetitle'));
              $alias = mb_substr($alias, 0, 50);
              $resource->set('alias', $alias);
              $resource->save();


              Everything works exactly i needed but i noticed issue with access to some aliases in site if this "module" is activated.
              Where i make a mistake?
              • What is the issue?
                If there is another alias with the same string, the MODx processor will throw an error which may not be in your log and it won't save the info to the db until the alias error is fixed. You will need to rewrite the save() function as below:

                if ($resource->save() == false) {
                    $validator = $resource->getValidator();
                    if ($validator->hasMessages()) {
                        foreach ($validator->getMessages() as $message) {
                            $this->addFieldError($message['field'],$this->modx->lexicon($message['message']));
                        }
                    }
                }


                This should log any validation errors to your error log.
                  • 48853
                  • 12 Posts
                  Quote from: dvstudiosinc at Nov 17, 2014, 03:49 PM
                  What is the issue?
                  If there is another alias with the same string, the MODx processor will throw an error which may not be in your log and it won't save the info to the db until the alias error is fixed. You will need to rewrite the save() function as below:

                  if ($resource->save() == false) {
                      $validator = $resource->getValidator();
                      if ($validator->hasMessages()) {
                          foreach ($validator->getMessages() as $message) {
                              $this->addFieldError($message['field'],$this->modx->lexicon($message['message']));
                          }
                      }
                  }


                  This should log any validation errors to your error log.
                  Thank you for help! Module is fully functional and work just fine.

                  If it'll be useful for someone, here it is:

                  System event OnBeforeDocFormSave
                  // Trim alias length
                  $alias = $resource->cleanAlias($resource->get('pagetitle'));
                  $alias = mb_substr($alias, 0, 50); // here you can change quantity of characters
                  $resource->set('alias', $alias);
                  $resource->save();
                  
                  if ($resource->save() == false) {
                      $validator = $resource->getValidator();
                      if ($validator->hasMessages()) {
                          foreach ($validator->getMessages() as $message) {
                              $this->addFieldError($message['field'],$this->modx->lexicon($message['message']));
                          }
                      }
                  }