Using modTemplateVarResource with a Default Value

How to handle the modTemplateVarResource object when the value for a particular resource might be set to the default value of the TV.

In the last article, we saw how to get the value of a TV using the modTemplateVarResource object. The code in that article will only work if the TV is never set to its default value. In this article, we'll see what to do if the TV might be set to its default value.

MODX logo

Getting a TV Value that Might be set to its Default Value

As with the code of the last article, all you need is the ID of the resource and the ID of the TV. You just need an added step to see if the TV is set to its default value. In that case, there will be no TVR.

$resourceId = 22;
$tvId = 12;
$val = '';  /* Initialize $val to an empty string */

/* Set the selection criteria */
$c = array(
    'contentid' => $resourceId,
    'tmplvarid' => $tvId,

/* Get the TVR */
$tvr = $modx->getObject('modTemplateVarResource', $c);

/* Make sure we got the TVR and get the value */

if ($tvr) {
    $val = $tvr->get('value');
} else {
    /*  No TVR. We have to get the default value of the TV */
    $tv = $modx->getObject('modTemplateVar', $tvId);

    /* Make sure we got the TV object */
    if ($tv) {
        $val = $tv->get('default_text');

return $val;

It's a good idea to include this extra step even if you're pretty sure all the TVRs exist. It doesn't add significantly to the execution time because whenever the TVR exists, the extra code won't execute.

Because we initialized $val to an empty string, if any step of the process fails, that's what will be returned. We didn't have to check the return value of the TVR because if the TVR exists, some non-default value has been set for the resource, and if that's not the case, we want the default value of the TV.

Be careful, though. If a non-empty default value has been set, and Allow Blank is true for the TV, a user can save the resource with the TV empty. In this case, because the value saved with the resource is not the default value of the TV, a TVR *will* be created. That means our snippet will return an empty string instead of the default value. One solution would be not to allow a blank value for the TV. Another would be to adjust the code above to return the default value if the TV value is empty.

Notice that we also do a sanity check to make sure the $tv object has been retrieved successfully before trying to call ->get on it. Without that check, PHP would throw an error any time the code failed to get the TV object.

Coming Up

In the next article, we'll see a practical application of this technique using the code as a postHook for a snippet like Login, Register, or FormIt.

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.