MODX User-related Resource Fields I

This is the first in a series of articles about handling the user-relatedMODX logo fields of a resource (createdby, publishedby, editedby, deletedby). Each field holds the ID of the user who performed the action. In this article, we’ll look at how to get the full name of the users who performed the action for the current page (and the full name of the current user as well).


You can use an output modifier to get the full name, but it’s a fairly slow and inefficient process. First, the output modifier will have to be parsed. Then MODX will get the user object (which you don’t actually need), and then it will get the user profile object (which you also don’t need if all you want is the fullname field). In this article, we’ll see a faster way to do it which requires no parsing and skips getting either the modUser or the modUserProfile object.

In all the cases, we’ll use the $modx->getValue() method for fast database access.

The secret to all these methods is that the user’s ID is in the internalKey field of the modUserProfile object. That means we can reference the profile directly without having or getting the user object.

Full Name of Current User

This method takes advantage of the fact that the current user object is always available as $modx->user. Put this tag where you want the full name to appear:

[code language=”html”]

Create a snippet called CurrentUserFullname with this code:

[code language=”php”]
/* CurrentUserFullname snippet */
$userId = $modx->user->get(‘id’);
$query = $modx->newQuery(‘modUserProfile’, array(‘internalKey’ => $userId));
return $modx->getValue($query->prepare());

The code queries the user profile based on the user’s ID and returns the value of the fullname field without retrieving the full modUser object. It also doesn’t retrieve the modUserProfile object. It just grabs the fulname field’s value.


Note that if a user has no full name listed in their profile, the snippet will return nothing. If this might be an issue, you can use the username as a backup by replacing the last line with this code:

[code language=”php”]
$output = $modx->getValue($query->prepare());
if (empty($output)) {
$output = $modx->user->get(‘username’);
return $output;

It won’t slow the snippet down by much, because if the full name is found, the extra code won’t execute. This technique will also work in the snippets below.


Full Name of User who Created the Current Page

This is just as easy as the last one, because the ID of the user who created the current page is in the createdby
field of the current resource.

[code language=”html”]

Create a snippet called FullnameCreatedby with this code:

[code language=”php”]
/* FullnameCreatedby snippet */
$userId = $modx->resource->get(‘createdby’);
$query = $modx->newQuery(‘modUserProfile’, array(‘internalKey’ => $userId));
return $modx->getValue($query->prepare());

Notice that the query and return part is the same as the first snippet. The only thing changed is the method for getting the user’s ID.


Other User-related Fields

The code above can be used to get the full name for the user who created, published, edited, or deleted the resource (though the last one isn’t much use). Just change createdby in the code to publishedby, editedby, or deletedby. All three hold the ID of the user who performed the named action. The “username” backup will work here as well. Remember that those fields could be empty if the resource is unpublished or has never been edited.


Coming Up

What if you want a more general-purpose snippet that will get a full name for *any* of the user-related fields? We’ll see how to do that in the next article.


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 *