Show Current Page Info

Displaying the name of the various people who worked on the current Resource, and the dates they performed the most recent action.

By Bob Ray
August 22, 2022
Show Current Page Info

In the previous article, we saw how to display the Editor and edit date of the current Resource with a Snippet. In this one, we’ll expand that process to include the creator and publisher of the Resource and the creation and publication dates.

Snippet Tag

We’ll call our Snippet DocInfo. Its tag gets a little more complicated because the Snippet will do more, and because it uses a Tpl Chunk:

[[! DocInfo?
    &show_people=`_created,_edited,_published`
    &show_dates=`_created,_edited,_published`
    &date_format`%A, %B %d, %Y`
    &tpl=`DocInfoTpl`
]]

The Snippet still needs to be called uncached (with the exclamation point) since the values will vary.

The DocInfoTpl Chunk

Here’s the code of the DocInfoTpl Chunk. You can leave out any parts of it you don’t want displayed, or you can leave them all there and just modify the properties in the Snippet tag to omit some of them. MODX will remove any placeholders that are not set before rendering the page. This second option is handy if you want to use the same Tpl Chunk, but show a different display on different pages.

<div class="DocInfo">
[[+_createdby]] [[+_createdon]]
[[+_publishedby]] [[+_publishedon]]
[[+_editedby]] [[+_editedon]]
</div>

The return value of the Snippet will replace the Snippet tag. It will look something like this:

Created: Bob Ray Monday, December 20, 2021
Published: Bob Ray Tuesday, December 21, 2021
Edited: Joe Blow Thursday, December 23, 2021

The Snippet Code

Here is the code of the DocInfo Snippet:

/* DocInfo 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 */
$showPeople = $modx->getOption('show_people', $config, 
    'created,published,edited', true);

$showDates = $modx->getOption('show_dates', 
    $config, 'created,published,edited, true');

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

$tpl = $modx->getOption('tpl', $config, 
    'DocInfoTpl', true);

/* Create an array of possible values to display */
$values = array('created','published','edited');

/* Initialize variables */
$output = '';
$placeholders = array();

/* Loop through the values and set placeholders */
foreach ($values as $value) {
    $fullName = '';
    $date = '';
    $formattedDate = '';

    /* See if we're getting the full name by examining the value
       of the &show_people property */
    if (strpos($showPeople, $value) !== false) {
        /* Add 'by' to $value to get field name */
        $field = $value . 'by';

        /* Get user Id for the current value */
        $userId = $doc->get($field);

        /* Get user profile */
        $profile = $modx->getObject($classPrefix . 'modUserProfile', array('internalKey' => $userId));

        /* Get fullname, if any */
        if ($profile) {
            $fullName = $profile->get('fullname');
        }
    }
    /* See if we're getting the date by examining the
       value of the &show_dates property */
    if (strpos($showDates, $value) !== false) {
        $date = $doc->_fields[$value . 'on'];
    }
    /* Set Caption and Placeholders if we have 
        the fullname or the date */

    if ($fullName || $date) {
        /* Remove underscore and capitalize first 
           letter for caption */
        $heading = ucfirst(str_replace('_', '', $value)) . ': ';

        /* Set person placeholder, adding prefix and suffix;
           note that fullname may be blank here, in which case
           only the heading will be added */
        $placeholders['_' . $value . 'by']  = $heading . $fullName;

        /* Get the formatted date */
        if (!empty($date)) {
            $formattedDate = strftime($dateFormat, $date);
            if ($formattedDate === false) {
                $formattedDate = 'Invalid Date Format';
            }
        }

        /* Set date placeholder adding prefix and suffix */
        $placeholders['_' . $value . 'on'] = $formattedDate;
    }
}

/* Plug placeholders into Tpl chunk and return it */
$output = $modx->getChunk($tpl, $placeholders);

return $output;

How It Works

This code is much more complicated than the code from the previous Snippet. The main problem is that if the user or date are empty, we don’t want to show anything, but if we have either one, we need to display the caption. We also want the option to display the user or the date alone, but with the caption.

This line will execute whether the $fullName variable is empty or not, as long as either the fullname or the date has been set. That way, we make sure that every line to be displayed begins with the caption, regardless of what it’s showing.

/* Set person placeholder, adding prefix and suffix */
$placeholders['_' . $value . 'by']  = $heading . $fullName;

The code is also complicated by the fact that our placeholders need a prefix, because MODX already sets placeholders for those fields. To prevent collisions, we’ve added an underscore prefix to the placeholders.

The values of the &show_people and the &show_dates properties in the Snippet tag, are the field names minus “by” or “on”. That way we can add those suffixes in the Snippet for either set to get the field values.

The comments explain what’s happening in the code. 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 placeholders don’t get set and MODX removes the placeholder tags.

As in the previous article, the final argument (false) tells MODX not to bother caching the results.

We’re also using the $_fields member of the Resource object as we did in the previous article.

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:

[[!DocInfo]]

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.