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

    I have a drop down menu called "Hello, Guest" which contains the login form. When I log in I want to change the name to "Hello, [username]". How can I achieve this?

    Thanks.
      • 37042
      • 384 Posts
      Search for a snippet by BobRay called Personalize. It allows you to display different chunks depending on the users logged in status. Username display is also a function.
        ...
        • 3109 ☆ A M B ☆
        • 894 Posts
        If you're using Login it comes with a Profile snippet which can allow you to access the users fields, more info here.
          Benjamin Marte
          Interactive Media Developer
          Follow Me on Twitter | Visit my site | Learn MODX
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          You should be able to do something with the Output Modifier
          [[!+modx.user.username]]

          By default the anonymous user is "(anonymous)" but there should be a way to change that to "guest".
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Well, well... this is not good. It's hard-coded! modx.class.php line 1207.
            'username' => '(anonymous)'

            The culture hasn't been initialized yet for dealing with the lexicons, so
            'username' => $this->lexicon('anonymous')

            doesn't work.

            Definitely shouldn't be done that way, though. I'm going to report this as a bug.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 3749
              • 24,544 Posts
              I would not advise changing the name of the (anonymous) user. Checking that is the fastest way to find out if a user is logged in and changing it might break some things.

              It would be safer to use a plugin attached to OnWebPagePrerender with:

              $output = &$modx->resource->_output; 
              $output = str_replace('(anonymous)','Guest',$output);
                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
                • 3109 ☆ A M B ☆
                • 894 Posts
                Here's a sample of how I do it using Logins Profile snippet.

                [[!Profile? &user=`[[!getUserId]]` &useExtended=`1`]]
                
                Welcome [[+fullname:empty=`Guest`]]
                


                And this is what my getUserId snippet has.

                <?php
                $user = $modx->getAuthenticatedUser($modx->context->get("key"));
                echo $user->id;
                


                The output modifier will display Guest is the fullname has no value (user is not logged in) without doing any PHP, I just love seeing all the different ways things can be accomplished in MODX it's what makes it awesome.

                Good luck.
                  Benjamin Marte
                  Interactive Media Developer
                  Follow Me on Twitter | Visit my site | Learn MODX
                  • 3749
                  • 24,544 Posts
                  If you're going to use a snippet, why not do the whole task there and cut down on the overhead?


                  [[!ShowUsername]]



                  /* ShowUsername snippet */
                  $output = "";
                  if ($modx->user->get('id') == 0) {
                      $output = 'Guest';
                  } else {
                      $output = $modx->user->get('username');
                  }
                  return $output;


                  Or, with the option to show the full name:

                  [[!ShowUsername? &fullName=`1`]]
                  


                  /* ShowUsername snippet */
                  $output = "";
                  $fullName = $modx->getOption('fullName', $scriptProperties, false);
                  if ($modx->user->get('id') == 0) {
                      $output = 'Guest';
                  } else {
                      if ($fullName) {
                          $profile = $modx->user->getOne('Profile');
                          if ($profile) {
                              $output = $profile->get('fullname');
                          }
                      } else {
                          $output = $modx->user->get('username');
                      }
                  }
                  if (empty($output)) {
                      $output = $modx->user->get('username');
                  }
                  return $output;
                  
                    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
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    And if you want this to actually be a link, you can use the snippet as part of the menu title of a resource.
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 4172
                      • 5,888 Posts
                      what about:

                      Hello [[!+modx.user.id:is=`0`:then=`Guest`:else=`[[+modx.user.username]]`]]
                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!