User-related Resource Fields IV

Using a Tpl Chunk to display user-related resource fields

In the last article, we used a single snippet to set placeholders for the various user-related resource fields of the current resource. In this one, we'll do the same thing with a Tpl chunk. The HTML will move into the Tpl chunk and we'll make some minor modifications of the code to make it work.

Using Tpl Chunks

Before we look at the chunk and the new snippet, let's take a look at how Tpl chunks are handled in MODX. You've probably used Tpl chunks with various MODX extras like Wayfinder and getResources, but it really helps to understand what's happening under the hood.

A Tpl chunk is nothing more than a regular MODX chunk that contains placeholders. There's nothing magic about it. The magic is in the MODX getChunk() method. The getChunk() method takes two arguments. The first one is the name of the chunk you want to retrieve. Imagine this tag and snippet combination:

/* ShowChunk snippet */
$output =  $modx->getChunk('MyChunk');
return $output;

When MODX sees the snippet tag, it calls the ShowChunk snippet. The snippet simply gets the contents of chunk named MyChunk, and returns it. The tag is replaced with the contents of the chunk. If you're at all familiar with PHP, you know that we could have uses a faster and more efficient one-line snippet:

return  $modx->getChunk('MyChunk');

Using getChunk() misses some of it's power. We said that getChunk() takes two arguments. The second one is optional but when it's used, it is an associative array that getChunk() uses to replace placeholders in the chunk before its content is returned.

An associative array is just a list of key and value pairs, like this:

$placeholders = array (
    'createdby' => 'Bob Ray',
    'editedby' => 'Joe Blow',
    'publishedby' => 'Jane Doe',

The keys are on the left, the values are on the right. If we call getChunk() like this:

$placeholders = array (
    'createdby' => 'Bob Ray',
    'editedby' => 'Joe Blow',
    'publishedby' => 'Jane Doe',

return $modx->getChunk('MyChunk', $placeholders);

MODX will retrieve the MyChunk Tpl chunk, but before returning it, it will step through each of the three members of the array and if it finds a placeholder in the chunk with the same name as the key, it replaces the whole placeholder tag with the value for that key. In other words, will be replaced with Bob Ray, and so on. If no placeholder with a given key is found, MODX does nothing with the value for that placeholder and moves on to the next member of the array.

Back to the Task at Hand

It's a short step from the example above to what we're trying to do. Here's the content of our Tpl chunk (MyChunk):

<p>Created By: [[+createdby]]</p>
<p>Published By: [[+publishedby]]</p>
<p>Edited By: [[+editedby]]</p>

Notice that this is the same code we used in the previous article, minus the snippet tag, which will go in the content section of our resource:


Now all we have to do is modify our previous code to create the array of keys and values and send it to getChunk()

/* GetFullnames snippet */

/* Create the array, with the values unset */
$fields = array(
    'createdby' => '',
    'publishedby' => '',
    'editedby' => '',

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

    /* Use username as a backup if the fullname is empty */

    if (empty($name)) {
        $query = $modx->newQuery('modUser', $userId);
        $name = $modx->getValue($query->prepare());
    $fields[$field] = $name;

return $modx->getChunk('MyChunk', $fields);

Our code hasn't changed much. We added the empty string as the value for each key. This is necessary because the array code we used in the previous article looked like this:

$fields = array(

That code creates this array:

$fields = array(
    0 = 'createdby',
    1 = 'publishedby',
    2 = 'editedby'',

Because we need the field names to be the keys of the array we send to getChunk(), not the values, we created the array with empty values. Doing this requires a change to the foreach() statement. We changed it to foreach ($fields as $field => $value) so that the $field variable would still hold the name of the field. The $value variable isn't actually used in the loop (it holds the empty string in each case), but without it, PHP would put the empty string in the $field variable and we'd get no results.

We could have used our original array code and created a new associative array, used a new variable name to send to getChunk() and changed the last two lines:

/* This would go above  the loop */
$newArray = array();

foreach($fields as $field) {
    /* ... */

    $newArray[$field] = $name;
return $modx->getChunk('myChunk', $newArray);

It's not necessary to create a whole second array, though, so we did it in a much more efficient way by modifying the original array to have empty values, filling them in the loop, and sending that array to getChunk() as the second argument.

Coming Up

The last few articles have looked at getting user information for the user-related fields of the current resource. Someday, you might want to get all the resources for a particular user and report on those resources. We'll look at how to do that in the next article.

Comments (0)

Please login to comment.