Using modTemplateVarResource in a postHook

Setting TV values with modTemplateVarResource based on user input from the Register form


In the last two articles, we saw how to get the value of a TV using the modTemplateVarResource object. In this one, we'll look at using that code in a postHook with another snippet.


MODX logo

The Concept

This example comes from a question by MODX forum member dubbs about setting two TVs on a project folder resource based on the users entries using a postHook on the Register snippet (part of the Login package). The folder's ID was 19 and the ID's of the TVs were 32 and 33.


The Code


/* Get the two values from the form */
$qualificiation_areyou = $hook->getValue('qualificiation_areyou');
$qualificiation_emailaddress = $hook->getValue('qualificiation_emailaddress');


/* Set TV value for the first TV from the form data */
$projectFolderID = 19;
$tvr = $modx->getObject('modTemplateVarResource', (array('contentid' => $projectFolderID, 'tmplvarid' => 32)));

if ($tvr) {
    $tvr->set('value', $qualificiation_areyou);

} else {
    $tvr = $modx->newObject('modTemplateVarResource');
    $tvr->set('contentid', $projectFolderId);
    $tvr->set('tmplvarid', 32);
    $tvr->set('value', $qualificiation_areyou);
}
    $tvr->save();


/* Set TV value for the second TV from the form data */
$tvr = $modx->getObject('modTemplateVarResource', (array('contentid' => $projectFolderID, 'tmplvarid' => 33)));

if ($tvr) {
    $tvr->set('value', $qualificiation_emailaddress);
} else {
    $tvr = $modx->newObject('modTemplateVarResource');
    $tvr->set('contentid', $projectFolderID);
    $tvr->set('tmplvarid', 33);
    $tvr->set('value', $qualificiation_emailaddress);
}
$tvr->save();

return true;

The code, with its comments, is fairly self-explanatory. Notice, though, that rather than returning the value as we did in the last article, we simply save the TVR. Another difference here is that if the TVR doesn't exist, we have to create it using $modx->newObject(). We return true at the end of the snippet. If we don't do this, other postHooks that come after this one in the list will not execute.

We've also put the array containing the search criteria as a second argument in the getObject() rather than in a separate array. This is a matter of personal preference. Doing it this way makes the code shorter, but arguably more difficult to read and more prone to typing errors.

Other Applications

Several MODX snippets accept postHooks, which are simply snippets that execute at the end of their operations. You could use similar code to set a TV value based on the user's entries in Login, or FormIt.

Coming Up

In the next article, we'll see another practical application of using the modTemplateVarResource object with a plugin when editing a resource.



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)