MODX User Resource Report I

In the last few articles, we saw how to display user information for the uMODX logoser-related fields of a single resource. In this series of articles, we’ll go in a slightly different direction and look at how to display information about a collection of resources acted on by the current user. As we did in the previous article, we’ll use a TPL chunk to format the output.

Getting the Resources

This couldn’t be much simpler. We can get the current user’s ID with this code:

[code language=”php”]
$userId = $modx->user->get(‘id’);

As we saw in the previous articles, the user-related fields of a resource (createdby, editedby, publishedby) hold the ID of the user who performed each action. It’s a short step from there to getting a collection of the user’s resources. For example, if we want all the resources created by the user, we can do this:

[code language=”php”]
$userId = $modx->user->get(‘id’);
$docs = $modx->getCollection(‘modResource’, array(‘createdby’ => $userId));

We’ll need a Tpl chunk to display the output and some more code to process each resource, but first, let’s look at an easy way to create the associative array we need for the placeholders in the Tpl chunk.



Every MODX object is descended from the basic xPDO object. That object has a method called toArray(), which means that every MODX object (including resources, users, chunks, snippets, plugins, templates, template variables, and lots more) inherits that method. Once you have the object, you can call its toArray() method.

The toArray() method creates exactly the kind of associative array that $modx->getChunk() is expecting. (If you need to review how $modx->getChunk() works, take a look at the previous article.) Basically, toArray() returns an array where the keys are the fields of the object and the values are the contents of those fields. Here’s an abbreviated version of the array toArray() returns for a resource:

[code language=”php”]

‘pagetitle’ => ‘MyPage’,
‘longtitle’ => ‘This is My Page’,
‘description’ => ‘This is my first page’,
‘alias’ => ‘mypage’,
‘createdby’ => 22,
‘content’ => ‘This is the content of my first page’,


toArray() doesn’t always return *all* the fields of the object (though it usually does). For the user object, for example, the toArray() method is overridden to remove the password and hash values of the user, so those are not returned.


The Snippet Tag

We’ll call our snippet UserResourceReport, so the tag will look like this:

[code language=”html”]


The Tpl Chunk

The Tpl chunk will contain placeholder tags for the resource fields we want to show. The snippet will actually set placeholders for all the fields returned by toArray(), so you can add or remove fields to show just by editing the Tpl chunk. All the resource fields will be in the array, but if you don’t set a placeholder for a given field, MODX will ignore it. We’ll call our Tpl chunk ResourceReportTpl:

[code language=”html”]
<p class="page_field">Description: [[+description]]</p>
<p class="page_field">Summary: [[+summary]]</p>

You can have as many resource fields as you like and format them however you want See this page for a full list of the resource fields.


The Code

Here’s the code to show the list of resources created by the current user:

[code language=”php”]
/* UserResourceReport snippet */
$userId = $modx->user->get(‘id’);
$docs = $modx->getCollection(‘modResource’, array(‘createdby’ => $userId));
$output = ”;

foreach($docs as $doc) {
$fields = $doc->toArray();
$output .= $modx->getChunk(‘ResourceReportTpl’, $fields);

return $output;

As you can see, there’s not much to the snippet. It really demonstrates the power of xPDO and MODX. It makes a single query to the database and will quickly produce a display of information about all the resources the user has created on the site. You may want to exclude deleted and unpublished resources. That’s easily done by changing the criteria used by getCollection():

[code language=”php”]
$c = array(
‘createdby’ => $userId,
‘deleted’ => false,
‘published’ => true,

$modx->getCollection(‘modResource’, $c);


Coming Up

If we want to change from resources created by the user, to resources published or edited by the user we need to change the code of our snippet. In the next article, we’ll send the field we want to use as an argument to the snippet.


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 *