We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36754
    • 21 Posts
    I have a rather general question about plugins and resources.

    in response to the modx event "OnBeforeDocFormSave" I can use

    $resource->set('attrname', 'value');


    but setting tv values won't work. (why?)

    On the other hand, in response to the modx event "OnDocFormSave" I can use

    $resource->setTVValue('tvname', 'value');


    but setting any resource attributes won't work.

    could anyone explain, why that is the case. or how it is done better?

    I want i.e. read a resource attribute, change a tv according to the contents of the attribute value, and change the attribute value itself at last.


    At the moment I am working with revo 2.3.3-pl. But I guess this is no version specific bug.

    Edit: Some more forum posts like the ones below that scratch the topic but I couldn't find a description how to do stuff like validation and autocorrections in the manager for resources via plugins. Any hints welcome.

    https://forums.modx.com/forums/thread/71048/setting-tv-value-via-plugin#dis-post-396921
    https://forums.modx.com/forums/thread/37615/solved-set-templatevariable-value-for-a-resource#dis-post-213146

    thank you in advance,
    ~d [ed. note: domindian last edited this post 11 years, 3 months ago.]
      • 3749
      • 24,544 Posts
      This isn't very efficient code, but it's easier to follow. I would attach it to OnDocFormSave so you don't have to worry about your changes being overwritten:

      $field = $resource->get('fieldName');
      if ($field == 'something') {
          $resource->setTVValue('tvName', 'Some value');
          $resource->set('fieldName', 'Some value');
          $resource->save();
      }
      


      Note that setTVValue() doesn't require a save, but you must save the resource for the field value changes to persist.
        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