Using modTemplateVarResource in a Plugin

Example code for using modTemplateVarResource to set a TV based the value of one or more other TVs

In the last article, we saw how to use the modTemplateVarResource object to set the value of a TV in a postHook on the Register snippet. In this one, we'll look at using similar code in plugin attached to OnDocFormSave.

MODX logo

The Concept

Suppose you want to set the value of a TV for a resource based on one or more other TVs for that Resource. In this example, we'll take the sum of the values of two TVs and put that sum in a third TV for the same resource. Let's assume that the first two TVs have IDs of 12 and 13, and the TV we want the sum in has the ID of 14. We'll use the modTemplateVarResource object to save the sum. We'll call our plugin "SaveSum". In the plugin, the $resource variable will contain the resource object, so we'll use its getTVValue() method to get the two values we'll be adding.

The Code

/* SaveSum plugin -- attached to the OnDocFormSave System Event */

/* Get the two values from the TVs to be added */
$value1 = $resource->getTVValue(12);
$value2 = $resource->getTVValue(13);
$docId =  $resource->get('id');

/* Compute the Sum */
$sum = (int) $value1 + (int) $value2;

/* Get the TVR for the sum TV (if it exists) */
$tvr = $modx->getObject('modTemplateVarResource', (array('contentid' => $docId, 'tmplvarid' => 14)));

if (! $tvr) { /* TVR doesn't exist -- create it */
    $tvr = $modx->newObject('modTemplateVarResource');
    $tvr->set('contentid', $docId);
    $tvr->set('tmplvarid', 14);

/* Set the value and save the TVR */
$tvr->set('value', $sum);


We could have pulled the first two TV values using the code from the previous articles, but since we already have the $resource object, it's a lot easier to use getTVValue(). It does add an extra function call and a test to see if the argument is a name or a number, but the time for that is negligible and speed isn't that important when saving a resource. If this were a plugin that affected page-load speed, we'd probably want to use the code from the previous articles.

Because getObject() returns null if it fails, we can test with if (! $tvr) to see if it was found. Note that we could have used the names of the TVs rather than their IDs in the getTVValue() calls, but using the IDs is significantly faster. It's important to remember, though, that when we set the tmplvarid field, we need to use an integer, not a name. Since this is a plugin, rather than a hook, we no longer need to return true at the end.

Coming Up

In the next article, we'll move on to a new topic: Using the user's full name in the Registration email.

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 high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)

Comments (0)

Please login to comment.