Set Resource Fields and TV Values in a Plugin

In previous articles, we looked at how plugins work and saw how to perform actions when a user is saved in the Manager. In this one, we’ll see some plugin code that acts when a resource is saved in the Manager.

There are lots of things you might want to do when a new resource is saved in the Manager. You might want to adjust or duplicate a field, say to make the description field match the introtext field. You might also want to adjust a resource field based on the value of a TV, adjust a TV based on a resource field, or adjust one TV based on the value of one or more other TVs.

MODX logo

The following example plugins would all be connected to the OnDocFormSave event. To use them, paste the code into a plugin and, on the “System Events” tab, check the box next to OnDocFormSave. Then, save the plugin. In a plugin attached to that event, the resource being saved is always available as the $resource variable.

If you’ve worked with resources in the front end, you’re probably used to using the $modx->resource variable. It’s a common mistake to use that variable in a plugin attached to a Manager event. In plugins that run in the Manager, the $modx->resource variable is not set. Instead, use $resource. Note that plugins attached to front-end events like OnWebPagePrerender usually *do* use the $modx->resource variable. You can see the variables that are available in most MODX events here.

Here’s a simple example that just writes whatever is in the introtext (summary) field to the description field.


/* Do nothing if it's not a new resource */
if ($mode !== modSystemEvent::MODE_NEW) {
    return '';

$introText = $resource->get('introtext');
$resource->set('description', $introText);
return '';


Dogs and Cats

Imagine that you want to set a particular TV value for new resources created directly under a particular folder, but only if certain other conditions are true. Let’s say your site has a listing of pets available for adoption. We’ll ignore the fact that you should really use a custom database table for this information and say that you have TVs for things like the breed, weight, and name of the animal. You want to set a TV called “Size” to Large if the animal’s weight is over 45 pounds, but only for dogs — cats have a different weight limit (15). You can tell whether the animal is a dog or cat by checking the ID of its parent. In our example, the “Dogs” folder has an ID of 12 and the “Cats” folder has an ID of 22.

You could just enter the Size TV manually every time a dog or cat is entered into the system, but why not let the computer do the work for you with this plugin attached to OnDocFormSave:


/* Do nothing if it's not a new resource */
if ($mode !== modSystemEvent::MODE_NEW) {
$dogFolderId = 12;
$catFolderId = 22;

/* Get the animal's weight */
$weight = $resource->getTVValue('weight');

/* Handle dogs */
if ($resource->get('parent') == $dogFolderId) {
    if ( (integer) $weight > 45) {
        $resource->setTVValue('Size', 'Large');

/* Handle cats */
if ($resource->get('parent') == $catFolderId) {
    if ( (integer) $weight > 15) {
        $resource->setTVValue('Size', 'Large');

return '';


Notice that we don’t have to save the resource in the example above, because we haven’t changed any of its fields.

The code above could be compressed into fewer lines with some PHP tricks. It might be a few milliseconds faster, but it would be much more difficult to understand and modify.

Obviously, you’d probably want to have more weight classes (i.e., Small, Medium, Large), but it would be easy to add code for them. You might also want to remove the code at the top of the plugin that prevents the plugin from executing for existing animals. That way, the weight description TV value would change when you save the resource any time that the animal’s weight changed enough to put them in a different weight class.

More Fun with Plugins

You can perform a lot of useful tricks with plugins. Plugins like the ones in this article can not only save you valuable time, they have the potential to speed up page loads dramatically. Any processing that can be performed once when a resource is saved, will save a ton of time if that processing then doesn’t have to be performed every time the page is loaded.

In the next article, we’ll look at a very cool method for putting the processed values of TVs into a resource’s introtext field so they don’t have to be parsed every time the page is loaded.


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

I am the author of MODX: The Official Guide and over 30 MODX add-on components. I host Bob's Guides, a source of valuable information for MODX users, and I've been very active in the MODX Forums with over 18,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *

7 + 6 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>