We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30585
    • 833 Posts
    I couldn't find a plug-and-play solution to get the current user's profile information in MODX Revolution, so I wrote this very simple snippet to achieve just that. There may be a package that accomplishes this, but my search didn't return anything.

    Anyway here it is. I called it currentUser

    Usage:
      [[!currentUser? &field=`email`]]
      [[!currentUser? &field=`color` &extended=`1`]]
    

    Snippet
    <?php
    /**
     * currentUser
     *
     * A simple snippet that returns profile and extended fields of the current user logged into the frontend
     * For a full list of fields see here: http://rtfm.modx.com/revolution/2.x/administering-your-site/security/users
     * 
     * @ author Treigh Pugh
     * @ copyright 2014 Treigh Pugh
     * @ version 1.0.0 - December 28, 2012
     * @ MIT License
     * 
     * OPTIONS
     * 
     * field - The field to retrieve - defaults to fullname.
     * extended - (Opt) Set to true to get extended fields. 
     *
     * Usage: 
     * 
     * [[!currentUser? &field=`email`]]
     * [[!currentUser? &field=`color` &extended=`1`]]
     * 
     **/
    
    // Set defaults
    
    $field    = $modx->getOption('field', $scriptProperties, 'fullname');
    $extended = $modx->getOption('extended', $scriptProperties, false);
    
    $profile = $modx->user->getOne('Profile');
    
    if ($extended) {
        // If &extended=`1`, get extended fields
        $fields = $profile->get('extended');
        $output = $fields[$field];
    } else {
        // Else, just get profile fields
        $output = $profile->get($field);
    }
    
    return $output;
    

    Please suggest any improvements or alternatives if necessary.
      A MODx Fanatic
      • 3749
      • 24,544 Posts
        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
        • 30585
        • 833 Posts
        Hahaha. This was hiding in plain sight. Wasn't it? Sharp eye BobRay!
          A MODx Fanatic
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          MODX provides the userinfo output modifier, which works with any tag that provides a user ID, such as [[*createdby]]. And for the current user, MODX supplies user placeholders.
          [[*createdby:userinfo=`fullname`]]
          [[+modx.user.id:userinfo=`extended.somefield`]]

          the extended.somefield syntax appears to only work with top-level attributes, it doesn't go into containers to get their attributes.
          [ed. note: sottwell last edited this post 11 years, 8 months ago.]
            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
            • 30585
            • 833 Posts
            Very enlightening indeed.
              A MODx Fanatic
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Hmmm. Reading the code, the userinfo output modifier also looks for "remote_data" - userinfo=`remote_data.somefield`. Now I wonder just what that is?
                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
                There are two fields in the modUser object:

                remote_key (string) - varchar 255
                remote_data (json) - text

                I think they are intended to store information used to log the user into some system other than MODX, but I'm not sure. I'm 99% sure that MODX doesn't use them for anything, so you could store information there.
                  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
                  Everybody seems to have forgotten about the user_settings table. It has several fields, but basically works with 'user' (user ID), 'key' and 'value' fields.

                  Interestingly, it contains an "editedon" field which could be useful for a lot of things. I've often wished that the user table had a createdon field.
                    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
                    You're right, I always do forget about them. Not only can you store data there and retrieve it fast, you can display it with a tag.

                    On the negative side, I suspect that updating the setting in code would be painfully slow (though reading it would be very fast -- I think it will already be in memory). I know it is like that for System Settings. Creating a new System Setting in code is positively glacial on my localhost install. I think it has to do with how the lexicon strings are handled.
                      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