In the previous article, we produced a list of resources created by the current user. In this one, we'll send a property in the snippet tag to tell the snippet what user-related field we want to search for and report.
The Snippet Tag
With the added property, our snippet tag will look like this::
[[UserResourceReport? &searchField=`createdby`]]
The Tpl Chunk
The Tpl chunk (ResourceReportTpl) hasn't changed:
<h3>pagetitle</h3>
<p class="page_field">Description: [[+description]]
<p class="page_field">Summary: [[+introtext]]
The Code
Here's the code of our snippet, modified to use the &searchField property. We'll set createdby as the default in case the user forgets to include the &searchField property.
/* UserResourceReport snippet */
$userId = $modx->user->get('id');
$searchField = $modx->getOption('searchField', $scriptProperties, 'createdby', true);
$docs = $modx->getCollection('modResource', array($searchField => $userId));
$output = '';
foreach($docs as $doc) {
$fields = $doc->toArray();
$output .= $modx->getChunk('ResourceReportTpl', $fields);
}
return $output;
As you can see, The addition of the new property doesn't change our snippet very much.
A Complete Report
Now that our snippet will get resources based on any field, we can produce a complete report for the user by doing something like this:
<h3>Resources You've Created</h3>
[[!UserResourceReport? &searchField=`createdby`]]
<h3>Resources You've Published</h3>
[[!UserResourceReport? &searchField=`publisheaby`]]
<h3>Resources You've Edited</h3>
[[!UserResourceReport? &searchField=`publishedby`]]
Coming Up
Our list of resources will generally be in the order that the resources were created. You might well want to sort them (usually by pagetitle). We'll see how to do that in the next article.
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.