Show Full Name of Page Editor

Displaying the name of the last editor and editing date of the current Resource.

By Bob Ray
August 15, 2022
Show Full Name of Page Editor

A while ago, a MODX Forum user asked for a way to show site visitors the full name of the last person to edit the current front-end page, and (optionally) the date the edit occurred.

This could be done with a fairly inefficient set of conditional output modifiers, but it’s pretty easy, and much faster, to do it with a custom Snippet. We’ll call our Snippet “ShowEditStatus”. Here is its tag:

[[!ShowEditStatus? 
    &show_full_name=`1` 
    &show_date=`1` 
    &date_format`%A, %B %d, %Y`
]]


The Snippet needs to be called uncached (with the exclamation point) since the values will vary. The return value of the Snippet will replace the Snippet tag. It will look something like this:

Edited by Bob Ray
Edited on Monday, December 20, 2021

The Code

Here is the code of the ShowEditStatus Snippet:

/* ShowEditStatus snippet */

/* Get the current resource as an object */
$doc = $modx->resource;

/* Save some typing */
$config = $scriptProperties;

/* Let the code run in both MODX 2 and MODX 3 */
$classPrefix = $modx->getVersionData()['version'] >= 3
    ? 'MODX\Revolution\\'
    :'';

/* Get the property values from the snippet tag */
$showFullName = $modx->getOption('show_full_name', $config, true);
$showDate = $modx->getOption('show_date', $config, true);
$dateFormat = $modx->getOption('date_format', 
    $config, '%A %B %d, %Y', true);

/* Initialize the output to an empty string */
$output = '';

/* Get the ID of the most recent editor (if any) */
$editorId = $doc->get('editedby');

if ($showFullName && $editorId) {
    $profile = $modx->getObject($classPrefix . 'modUserProfile',
        array('internalKey' => $editorId), false);
    if ($profile) {
        $fullName = $profile->get('fullname');
        if (!empty($fullName)) {
            $output .= "\nEdited by " . $fullName . '';
        }
    }
}

$editedOn = $doc->_fields['editedon'];
if ($showDate && $editedOn) {
        $output .= "\nEdited on " . 
            strftime($dateFormat, $editedOn) . '';
}

return $output;


How It Works

The code is fairly straightforward and the comments explain what’s happening. If the “show” properties are set, and the relevant field is not empty, the Snippet adds the information to the $output variable, which is returned at the end of the Snippet. If neither value is available, the Snippet returns the empty string.

We get the profile with $modx->getObject(), using the class prefix we set near the top of the code. The user’s ID is held in the internalKey field of the profile. The final argument (false) tells MODX not to bother caching the results.

We could fall back to the username if the fullname field is empty, but for security, it’s not a good idea to expose usernames on the front-end of the site.

There is one unusual trick in the part that gets the date. You might expect that we’d use $doc->get('editedon') to retrieve the date the Resource was last edited. The problem with that is that although the date is stored as a unix timestamp in the database, get() returns a human-readable string (probably not formatted the way we want it).

Rather than converting the retrieved string to a timestamp with strtotime(), then turning it back into human-readable form with strftime(), we get the timestamp directly from the internal fields of the Resource ($doc->_fields['editedon']).

This is cheating, because that member is meant to be used internally and not referenced from the outside, but it works and is very fast. Just be aware that it could stop working down the road if the structure of the Resource object changes, because it’s not part of the Resource object’s public interface. The complete list of formatting codes for the date/time can be found here. You could modify the format string to include the time if you want to display that.

Notice that the values of the Snippet properties in the example above are all set to their default values. If you’re happy with those values, you can call the Snippet without including them:

[[!ShowEditStatus]]

Modifications

If you use this code, you’ll probably want to make some changes in the output format. If you are a purist about separating form from content, you may want to create a Tpl Chunk to display the output and/or add a &tpl property to the Snippet tag. You could also set placeholders in the Snippet so that the output could be placed anywhere on the page using code like this:

$modx->setPlaceholder('full_name', $profile->get('fullname'));
$modx->setPlaceholder('edited_on', strftime($dateFormat, $editedOn);

Then these two placeholders could go anywhere on the page:

[[+full_name]]
[[+edited_on]]

Generalizing the Snippet

It may have occurred to you that this technique could be used to display other date fields like createdon and publishedon and the person who committed each action. In the next article, we’ll see a more flexible version of the Snippet that will show any combination of the fields, set placeholders for them, and offer the option to use a Tpl Chunk.


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.