Show Full Name of Editor and Date of Edit for Resources in MODX

A MODX user asked for a way to show the full name of the last person to edit the current page, or nothing at all if the page had never been edited. You could do this with a fairly complex and inefficient set of conditional output modifiers, but it’s actually easier and *much* faster to do it with a custom snippet. We’ll call our snippet “ShowEditStatus”. Here is its tag:

[[!ShowEditStatus? &showFullName=`1` &showDate=`1` &dateFormat`%A %B %d %Y`]]

MODX logo

The Code

Here is the code of the ShowEditStatus snippet:

/* ShowEditStatus snippet */

$doc = $modx->resource;
$config = $scriptProperties;

$showFullName = $modx->getOption('showFullName', $config, true);
$showDate = $modx->getOption('showDate', $config, true);
$dateFormat = $modx->getOption('dateFormat', $config, '%A %B %d %Y');

$output = '';

if ($showFullName) {
    $editorId = $doc->get('editedby');
    if (!empty($editorId)) {
        $profile = $modx->getObject('modUserProfile', array('internalKey' => $editorId));
        if ($profile) {
            $output = "\n<p>Edited by " . $profile->get('fullname') . '</p>';
        }
    }
}

if ($showDate) {
    $editedOn = $doc->_fields['editedon'];
        if (!empty($editedOn)) {
            $output .= "\n<p>Edited on " . strftime($dateFormat, $editedOn) . '</p>';
        }
    }
return $output;

How it Works

The code is fairly straightforward. If the “show” properties are set, it gets the appropriate field and adds it to the output. 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.

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 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]]

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

I am the author of MODX: The Official Guide and over 30 MODX add-on components. I host Bob's Guides, a source of valuable information for MODX users, and I've been very active in the MODX Forums with over 14,000 posts.

Leave a Reply

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


1 + 2 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>