We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 52534
    • 13 Posts
    Hey all,

    I'm looking for a plugin to change the default behavior of the auto-generated Alias field. I'd like it to auto-generate from the Longtitle field instead of the Pagetitle, but keep all the other default functionality, ie. Auto-fill if blank but allow for manual change, duplicate detection, character cleaning, etc...

    I thought I'd try my hand at PHP (not fluent) and wrote the following:
    <?php
    if ($resource->get('alias') == ''){
        $resource->set('alias',$resource->cleanAlias($resource->get('longtitle')));
        $resource->save();
    }
    return;
    


    However, this does not work as planned. Thoughts?

    This question has been answered by BobRay. See the first response.

      Front End Designer + Developer for Eternity
      • 3749
      • 24,544 Posts
      That looks right to me, though I would change the if to:

      if (empty($resource->get('alias'))) {
      
      }


      Try setting the automatic_alias System Setting to "No", since you are handling that yourself.

      What System Event is your plugin attached to?
        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
        • 52534
        • 13 Posts
        <?php
        if (empty($resource->get('alias'))) {
            $resource->set('alias',$resource->cleanAlias($resource->get('longtitle')));
            $resource->save();
        }
        return;


        I changed the if to your suggestion, however that locks the site up in "forever saving" loop.

        I have the plugin on the OnBeforeDocFormSave thinking I could beat the empty Alias validation... however an empty Alias prevents me from saving instead of taking the Longtitle as it should.
          Front End Designer + Developer for Eternity
        • discuss.answer
          • 3749
          • 24,544 Posts
          Sorry, my bad. You can't use empty() inside an if statement like that unless you have PHP 5.5. It should be:

          $a = $resource->get('alias');
          if (empty($a)) {




          If that doesn't work, it's kind of hacky, but you might be able to turn automatic_alias back on, then in a plugin connected to OnDocFormSave, detect aliases auto-generated from the pagetitle, and convert them. The down side would be that the user could never get the pagetitle version even if they wanted it.


          (untested)
          $savedAlias = $resource->get('alias');
          $pagetitle = $resource->get('pagetitle');
          if ($savedAlias === $resource->cleanAlias($pagetitle)) {
             $resource->set('alias', $resource->cleanAlias($resource->get('longtitle')));
             $resource->save();
          }
          [ed. note: BobRay last edited this post 10 years, 2 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
            • 52534
            • 13 Posts
            That second "untested" version works great! Thank You!
              Front End Designer + Developer for Eternity
              • 13428 ☆ A M B ☆
              • 1,031 Posts
              You should change $pagetitle = $resource->get('pagetitle'); to $pagetitle = $resource->get('longtitle'); in the second line. Otherwise the alias is permanently recalculated.

              $savedAlias = $resource->get('alias');
              $pagetitle = $resource->get('longtitle');
              $cleanedAlias = $resource->cleanAlias($pagetitle);
              if ($savedAlias !== $cleanedAlias) {
                 $resource->set('alias', $cleanedAlias);
                 $resource->save();
              }


              Any maybe you want to include in a somehow different 'if empty' line, since you can't set the alias by yourself without a different value than the cleaned longtitle:

              $savedAlias = $resource->get('alias');
              if ($resource->get('alias') == '') {
                 $resource->set('alias', $resource->cleanAlias($resource->get('longtitle')));
                 $resource->save();
              }
              [ed. note: Jako last edited this post 10 years, 2 months ago.]
                • 52534
                • 13 Posts
                Thanks Jako.

                However, Bob's works like a charm.

                Your first example doesn't allow for a manual Alias to be entered and override the Longtitle auto-fill.
                You second example doesn't work due to the Alias' validator. It won't let the resource save or the plugin run if the Alias is blank on Save.
                  Front End Designer + Developer for Eternity
                  • 3749
                  • 24,544 Posts
                  My version has its flaws too. wink

                  If you enter a manual alias that happens to match the auto-generated one from the pagetitle, it will be converted to one based on the longtitle.
                    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
                    • 13428 ☆ A M B ☆
                    • 1,031 Posts
                    Sorry Bob, I obviously forgot that the generation of the default alias runs before the plugin and your alias generation runs only when the alias was generated from the pagetitle.
                      • 3749
                      • 24,544 Posts
                      No problem, Jako, I'm misreading code around here all the time. wink
                        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