Understanding the modTemplateVarResource Object

Getting the value of a TV for a particular resource via the modTemplateVarResource object without touching the modTemplateVar (TV) object.


Template Variables (TVs) in MODX are an odd beast because they can have a different value for each resource. In this article, we'll look at how that happens and see a way of setting that value quickly without ever accessing the TV object itself.


MODX logo

The modTemplateVarResource Object

The modTemplateVar (TV) object contains information about the TV itself — ID, type, category, input options, default value, and so on. What it doesn't contain is the value of the TV for any specific resource. That's in a separate table accessed via the modTemplateVarResource (TVR) object. The table that stores the TVRs is called modx_site_tmplvar_contentvalues.

The modTemplateVarResource (TVR) object has only three useful fields. tmplvarid (the ID of the TV), contentid (the ID of the resource), and value (the value of the TV for that resource). When you set a TV for a specific resource on the "Create/Edit Resource" panel in the Manager and save the resource, MODX first checks to see if the value entered is the default value of the TV. If it is, MODX does nothing. No TVR is created. This is important to remember, because when you're looking for specific TV value for a given resource using the modTemplateVarResource object, if the TV is set to its default value, you won't find it.


If you will be accessing your TV values in code, you can speed things up by giving the TV no default value (this will only work if the TV value is set for every resource). That way, there will always be a TVR with the value of that TV no matter which resource you want the value for, so you can count on getting the value through the modTemplateVarResource object. Otherwise, you'll have to take the time to check whether the TVR exists, and if it doesn't, you'll have to get the TV object and pull its default value.


Getting a TV Value

If there is a TVR for every resource for a given TV, all you need to get a particular resource's value for that TV is the ID of the resource and the ID of the TV.

$resourceId = 22; /* ID of the resource */
$tvId = 12;  /* ID of the TV */
$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');
}

return $val;

If the TVR object doesn't exist, $val the code will return an empty string, since we set $val to that at the top. It's a good practice to make sure that a known value will be returned if someone forgets to set the TV value for a particular resource. It's also critical that the $tvr->get() statement will only execute if the TVR is found. Otherwise, PHP will throw an error for any resource where the value is not set or is set to the TV's default value.

If the TVR object does exist, the code will return its value field. Note that no processing will be done on the TV value, so this will only work on TVs where the raw value can be used. If the TV's value is @INHERIT, for example, this code will return the string, "@INHERIT."


Coming Up

As we noted above, this will only work if the TV is never set to its default value. In the next article, we'll see how to handle things if some of the TVs are set to their default values.


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.

  (Login)