User-related Resource Fields Part 2

A general-purpose snippet to deal with user-related resource fields.

By Bob Ray  |  October 3, 2024  |  3 min read
User-related Resource Fields Part 2

In my last article, we saw how to get the full name (or username) of 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:

[[!GetFullname]]
[[!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:

/* 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));
$query->select('fullname');
$output =  $modx->getValue($query->prepare());

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

if (empty($output)) {
    $query = $modx->newQuery('modUser', $userId);
    $query->select('username');
    $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:

$userId = $modx->resource->get($type);

is exactly the same as doing this:

$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.


Bob Ray is the author of the MODX: The Official Guide and dozens of MODX Extras including QuickEmail, NewsPublisher, SiteCheck, GoRevo, Personalize, EZfaq, MyComponent and many more. His website is Bob’s Guides. It not only includes a plethora of MODX tutorials but there are some really great bread recipes there, as well.