We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39993
    • 8 Posts
    I'm trying to figure if I'm able to set the value of a TV from a script. I was attempting to use the code below...

    $tv = $modx->getObject('modTemplateVar',array('name'=>$newThumbnailName));
    $val = $tv->setValue('path/to/img/'.$newThumbnailName);


    ...but then I realized I'm not specifying a page to set this TV on. Is it possible to use xPDO/API to set the value of a TV on a specified page?

    Thanks for any clarification.

    This question has been answered by multiple community members. See the first response.

    • discuss.answer
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You need the ID of the resource, then you can set the TV value for the resource, using setTVValue().

      $resource = $modx->getObject('modResource', 42);
      $resource->setTVValue('<tvName>', '<value>');

        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
      • discuss.answer
        • 3749
        • 24,544 Posts
        If you have the TV ID and the Resource ID, this will be a little faster, since you don't have to retrieve either the resource or the TV:


        $tvId = 12;
        $resourceId = 23;
        
        $tvr = $modx->getObject('modTemplateVarResource',array ('tmplvarid'=>$tvId,'contentid' = $resourceId));
        if (!$tvr) {
            $tvr = $modx->newObject('modTemplateVarResource');
            $tvr->set('tmplvarid', $tvId);
            $tvr->set('contentid', $resourceId);
        }
        $tvr->set('value', 'Some Value');
        $tvr->save();



          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 39993
          • 8 Posts
          Quote from: sottwell at May 14, 2015, 01:03 AM
          You need the ID of the resource, then you can set the TV value for the resource, using setTVValue().

          $resource = $modx->getObject('modResource', 42);
          $resource->setTVValue('<tvname>', '<value>');


          Thank you guys for the quick responses. In my particular case, sottwell's method works best as I'm iterating through incremented TV names via a foreach loop. Using TV ID's would be much harder to automate. However, I'm sure I'll use both variations on different projects. Thanks again for your help.
            • 3749
            • 24,544 Posts
            Susan's method is much more convenient for your use-case and I think the speed penalty is probably very small.
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 39993
              • 8 Posts
              Quote from: BobRay at May 14, 2015, 10:09 PM
              Susan's method is much more convenient for your use-case and I think the speed penalty is probably very small.

              Well, for the most part yes. The load shouldn't ever be an issue or on a rare occasion. I'm pulling content from an external json file and storing some of it's contents in TV's. It's not a whole lot, but it does generate a load. The upside to this though is I only need to run my script to update once a week and it will be at an obscure hour of the night. So for that minute the script is running, my site may suffer, but once the information is stored it'll load fast. This is all in theory at least... we'll see how it performs in the end.