MODX User-related Resource Fields II

In the last article, we saw how to get the full name (or username) oMODX logof the user for any of the user-related fields, but to change the field, we had to rewrite the code of the snippet. In this article we’ll see a snippet that will handle the current user plus any other user-related field for the current resource.

We’ll send the name of the field we want to render as a property in the snippet tag.

The Tag

The snippet tag we use will look like one of these:

[code language=”html”]
[[!GetFullname? &type=`current`]]
[[!GetFullname? &type=`createdby`]]
[[!GetFullname? &type=`publishedby`]]
[[!GetFullname? &type=`editedby`]]
[[!GetFullname? &type=`deletedby`]]

The first two will get the current logged-in user’s full name. The second one may be a few milliseconds slower, but I prefer it because it’s easier to see what it does. The others will get the full name of the user who performed the action described in the &type property. We’ll also add a section to use the username as a backup if there’s no full name in the profile.


The Snippet

Here’s the snippet code:

[code language=”php”]
/* GetFullname snippet */

/* Get the property value; use ‘current’ as the default */
$type = $modx->getOption(‘type’, $scriptProperties, ‘current’, true);

/* Get the user’s ID */
if ($type == ‘current’) {
$userId = $modx->user->get(‘id’);
} else {
$userId = $modx->resource->get($type);

/* Perform the query to get the full name */
$query = $modx->newQuery(‘modUserProfile’, array(‘internalKey’ => $userId));
$output = $modx->getValue($query->prepare());

/* Use username as a backup if the fullname is empty */

if (empty($output)) {
$query = $modx->newQuery(‘modUser’, $userId);
$output = $modx->getValue($query->prepare());
return $output;


We’ve used separate code to get the user’s ID in the case of the current user, because that user ID is in a different place. Notice, though that it doesn’t change the query, which only needs the user ID and the field (fullname) that we want to retrieve. If we had put the query inside that first if statement, we would have to include the same query code twice. That would slow things down for no reason and make the code more difficult to read.

The rest of the code is pretty self-explanatory, but you might be wondering about $modx->user->get($type) line. Because we’ve used the name of the field we want in our &type property (which goes to the $type variable), we’re able to use that variable in getting any of the field values. If &type=`editedby`, for example, this code:

[code language=”php”]
$userId = $modx->resource->get($type);

is exactly the same as doing this:

[code language=”php”]
$userId = $modx->resource->get(‘editedby’);

By setting the variable to match the property value and using the actual field names for the property value, we can get the field containing the user ID for the user who performed any of the actions.


Coming Up

The example above is more flexible than the one in the previous article, but it does have a drawback. What if you want to list the full name for more than one of the user-related resource fields? You’d have to call the snippet once for each field, with all the overhead of a snippet call for each one. In the next article, we’ll see how to do it for multiple fields.


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or
better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *