We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • I seem to hitting some snags with a plugin. I want it to set a TV value when the document is saving, but the page save stalls.

    This works, fired OnBeforeDocFormSave:
    <?php
    $modx->log(modX::LOG_LEVEL_ERROR, 'test...');


    But as soon as I try to read some values from the resource being saved, the save operation just stalls:
    <?php
    $resourceId = $modx->resource->get('id');
    $modx->log(modX::LOG_LEVEL_ERROR, $resourceId);


    No errors, nothing...

    Any ideas?
      • 3749
      • 24,544 Posts
      Try using this:

      $resourceId = $id;


      Be be aware that if it's a new resource, the ID will be 0 because it doesn't have an ID yet.

      If you need the ID, you should be using OnDocFormSave instead. In that event these are all available directly:

      $resource
      $id
      $mode
        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
      • Thanks Bob. Hmm... this is proving difficult to troubleshoot, because I can't print the $resource in the log... the saving just stalls.

        Maybe you can recommend a better way to do this... I need to set the value of one TV based on the values of a couple other TVs during the save event.
          • 3749
          • 24,544 Posts
          The most efficient way to do that in Revo is in a plugin attached to OnDocFormSave. You can get the templateVarResource objects directly, do your magic and save the appropriate TVR.

          A modTemplateVarResource object has three fields.


          • tmplvarid (ID of the TV)
          • contentid (ID of the resource)
          • value (Value of the TV for that resource)

          In a plugin attached to OnDocFormSave, get each of the three TVRs like this:

          <?php
          $tvId = 12;
          /* $id will be the resource ID */
          $tv1 = $modx->getObject('modTemplateVarResource',array ('tmplvarid'=>$tvId,'contentid' = $id));
          $val = $tv1->getValue();


          Once you have the value for all three, set the value for the TV you want to change and save it (let's say it's $tv3 and the value you want to save there is in $newValue):

          <?php
          $tv3->set('value', $newValue);
          $tv3->save();
          


          And you're done. No need to mess with the resource object or the TV object at all.

          FYI, Plugins with syntax errors will often just hang -- so it really helps to have a good code editor like NetBeans, PhpEd, or PhpStorm that highlights syntax errors. When a plugin hangs, you can paste the code into the code editor and look for errors.

          [ed. note: BobRay last edited this post 12 years, 6 months ago.]
            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
          • Hmm... I'm hanging all over the place, even with my code editor. Does this look right to you?

            <?php
            $tv50 = $modx->getObject('modTemplateVarResource',array('tmplvarid'=>50,'contentid' => $id));
            $tv50->set('value', 'test...');
            if ($tv50->save() == false) 
            {
               $modx->log(modX::LOG_LEVEL_ERROR, 'Failed to set Set the comparison_date.');
            }

              • 26903
              • 1,336 Posts
              You can get a TV value by using the getTVValue method on the resource, in your case :-
              $modx->resource->getTVValue('TV Name')

              There's also a corresponding setTVValue that takes a second value parameter method.

                Use MODx, or the cat gets it!
              • Man, that's hanging too. Just a one-liner kills the saving process:

                <?php
                $modx->resource->setTVValue('comparison_date', 'test...');


                I'm baffled why this is proving so difficult... there's not even a good plan B for this one.
                  • 26903
                  • 1,336 Posts
                  Try $resource->setTVValue('comparison_date', 'test...');
                    Use MODx, or the cat gets it!
                    • 3749
                    • 24,544 Posts
                    That should do it. Because you're in the back end, there is no $modx->resource set, but $resource is passed to the OnDocFormSave event.

                    $resource->getTVValue() and setTVValue() should definitely work. I think using the TVRs will involve fewer steps behind the scenes, but the getTVValue() and setTVValue() code is easier to follow.
                      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
                    • Nope, tried that too:

                      <?php
                      $resource->setTVValue('comparison_date', 'test...');
                      


                      Saving still hangs. It's only tied to the OnDocFormSave event.... any other ideas? I'm seriously baffled why this is so difficult.

                      I should note that this is 2.0.8... gonna try to upgrade...