<?php $tv = $modx->getObject('modTemplateVar', array('name' => 'SomeTemplateVarOfMine')); if ($tv) { // get the unprocessed value for a resource $value = $tv->getValue($resource->get('id')); // get the processed output for a resource $output = $tv->renderOutput($resource->get('id')); // change the value for a resource $changed = $tv->setValue($resource->get('id'), 'new value'); } ?>
<?php $tv = $modx->getObject('modTemplateVar', array('name' => 'SomeTemplateVarOfMine')); if ($tv) { // get the unprocessed value for a resource $value = $tv->getValue($resource->get('id')); // get the processed output for a resource $output = $tv->renderOutput($resource->get('id')); // change the value for a resource $changed = $tv->setValue($resource->get('id'), 'new value'); } ?>
// change the value for a resource
$changed = $tv->setValue($resource->get('id'), 'new value');
$tv->save();
I’m sorry, what code are you using to define $resource or $tv? Some context would be useful as this code definitely works for me to set a value for a specific resource...
<br /><br />I can’t get this to have any effect. The original TV is unchanged and there’s nothing in the error log.
// change the value for a resource
$changed = $tv->setValue($resource->get('id'), 'new value');
$tv->save();
$resource = $modx->resource;
$resource->save()
$tv->save(); $resource->save();
Be careful, that makes a new copy of the $modx->resource and will not reflect changes back to the original instance, though that is definitely not involved in this issue. $modx->resource always represents the currently requested Resource. You can get any resource back from getObject().
$resource = $modx->resource;
...
Maybe $modx->resource isn’t the equivalent of what you obtain with getObject(). It seems like it should be.
$tv->save() is all that should be necessary after calling $tv->setValue(). I’m not sure what problem you are encountering...maybe some more context would help me visualize it? Sorry...
I also tried:
$resource->save()
and
$tv->save(); $resource->save();
$resource = $modx->resource; $tv = $modx->getObject('modTemplateVar', array('name' => 'BackgroundImage')); if ($tv) { // get the processed output for a resource $output = $tv->renderOutput($resource->get('id')); // change the value for a resource $changed = $tv->setValue($resource->get('id'), 'new value'); $tv->save(); } else { $output = "TV Not Found"; } return $output;