MODX User-related Resource Fields IV

In the last article, we used a single snippet to set placeholders for tMODX logohe 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:

[code language=”html”]
[[ShowChunk]]
[/code]

[code language=”php”]
/* ShowChunk snippet */
$output = $modx->getChunk(‘MyChunk’);
return $output;
[/code]

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:

[code language=”php”]
return $modx->getChunk(‘MyChunk’);
[/code]

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:

[code language=”php”]
$placeholders = array (
‘createdby’ => ‘Bob Ray’,
‘editedby’ => ‘Joe Blow’,
‘publishedby’ => ‘Jane Doe’,
);
[/code]

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

[code language=”php”]
$placeholders = array (
‘createdby’ => ‘Bob Ray’,
‘editedby’ => ‘Joe Blow’,
‘publishedby’ => ‘Jane Doe’,
);

return $modx->getChunk(‘MyChunk’, $placeholders);
[/code]

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, [[+createdby]] 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):

[code language=”html”]
<p>Created By: [[+createdby]]</p>
<p>Published By: [[+publishedby]]</p>
<p>Edited By: [[+editedby]]</p>
[/code]

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:

[code language=”html”]
[[!getFullNames]]
[/code]

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

[code language=”php”]

/* 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));
$query->select(‘fullname’);
$name = $modx->getValue($query->prepare());

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

if (empty($name)) {
$query = $modx->newQuery(‘modUser’, $userId);
$query->select(‘username’);
$name = $modx->getValue($query->prepare());
}
$fields[$field] = $name;
}

return $modx->getChunk(‘MyChunk’, $fields);
[/code]

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:

[code language=”php”]
$fields = array(
‘createdby’,
‘publishedby’,
‘editedby”,
);
[/code]

That code creates this array:

[code language=”php”]
$fields = array(
0 = ‘createdby’,
1 = ‘publishedby’,
2 = ‘editedby”,
);
[/code]

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:

[code language=”php”]
/* This would go above the loop */
$newArray = array();

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

$newArray[$field] = $name;
}
return $modx->getChunk(‘myChunk’, $newArray);
[/code]

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.

 


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 *