We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5143
    • 116 Posts
    Hi,

    I've been trying for several hours to write a simple revo(2.2.14) plugin that sets the value of a TV when the user saves a resource. For some reason I can't get anything to work; the console generally logs "Uncaught #<Object>" and the Modx saving progress bar fails to stop unless I do a page refresh.

    There's been a number of variations so far, but the gist of the code is as follows:

    $id = $resource->get('id');
    $tv = $modx->getObject('modTemplateVar','Project-Logo-Inverted');
    $newValue = 'TestString';
    
    if($resource->get('template') == 2){
    	$invertedTv->setValue($modx->resource->get('id'), $newValue);
    	$invertedTv->save();
    } else {
    	return 'false';
    };


    Any help would be hugely gratefully received.

    Chris
      • 3749
      • 24,544 Posts
      Your problem is this line:

      $tv = $modx->getObject('modTemplateVar','Project-Logo-Inverted');


      It would work with the ID of the TV as the second argument, but if you're not using the ID, you need to specify the field you're sending like this:

      $tv = $modx->getObject('modTemplateVar',array('name' => 'Project-Logo-Inverted'));


      FYI, I think you could do it more simply like this in a plugin connected to OnDocFormSave:

      if($resource->get('template') == 2){
         $resource->setTVValue('Project-Logo-Inverted', $newValue);
      }
      return '';
        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
        • 5143
        • 116 Posts
        A thousand thankyous.

        I'm not sure how I missed this configuration, but it indeed works.
          • 3749
          • 24,544 Posts
          Glad I could help. Thanks for reporting back. smiley
            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