User-related Resource Fields Part 3

An improved snippet to get the full name for any of a resource's user-related fields.

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

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.

The Wrong Way

Using the snippet from the previous article, we could display user full names for multiple fields like this:

Created By: [[!GetFullname? &type=`createdby`]]
Published By: [[!GetFullname? &type=`publishedby`]]
Edited By: [[!GetFullname? &type=`editedby`]]

This will definitely work, but we're calling the snippet three times for no good reason. That's ridiculously inefficient since the snippet could easily get all three names for us if we rewrote it.

Another Way

This isn't the best way, but it's definitely a step forward. We'll use placeholders for the names and set them in the snippet. Here's the HTML code:

[[!getFullNames]]

Created By: [[+createdby]]
Published By: [[+publishedby]]
Edited By: [[+editedby]]

And here's the snippet code:

/* GetFullnames snippet */

$fields = array(
    'createdby',
    'publishedby',
    'editedby'
);

/* Perform the query to get the full name for each field */
foreach ($fields as $field) {
    $userId = $modx->resource->get($field);
    if (!empty ($userId)) {
        $query = $modx->newQuery('modUserProfile', array('internalKey' => $userId));
        $query->select('fullname');
        $name =  $modx->getValue($query->prepare());

        /* Use username as a backup if the fullname is empty */
        if (empty($name)) {
            $query = $modx->newQuery('modUser', $userId);
            $query->select('username');
            $name = $modx->getValue($query->prepare());
        }
    } else {
        /* Field is empty */
        $name = '(empty)';
    }

    $modx->setPlaceholder($field, $name);
}

The code simply loops through the fields, getting the fullname or username for each one and setting the placeholder for each one. We first check to make sure the field has a non-empty value, since the page might not ever have been edited or published.

Because we used the field names for the placeholder names, we can use the $field variable to get the user ID and to set the placeholders. We could have used different placeholder names (e.g., createdby.user, but it would make the code more lengthy and less efficient.

Coming Up

It may have occurred to you that this use case above is crying out for a Tpl chunk. In the next article, we'll use a Tpl chunk to make things a little more efficient, easier to maintain, and to demonstrate how to use Tpl chunks in MODX code.


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.