We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11285
    • 6 Posts
    Hello everybody!

    I’m trying to write a Plugin, that resizes all images of a new resource in a very easy way.

    Everything works fine, but I have one "small" problem:
    One image should be used as a teaser-image for the resource. It should be possible to choose a special image for this as a TV. But if the TV remains empty, I want to put a generated value in this TV.

    The plugin is called on OnBeforeDocFormSave in Revolution 2.0.7-pl

    So I tried a lot like this:
    global $tmplvars;
    [...] // some code that works fine
    $tmplvars[][4] = $teaserimage; // put $teaserimage to TV with the ID 4 - but this doesn't work. 
    


    I searched the web for over 2 hours, but didn’t find something that seems to work in revolution.

    Any ideas how I can set/ update a TV on OnBeforeDocFormSave?

    Btw: Perhaps anyone can tell me an easy way to print out the values of Arrays when testing Plugins? This would make the devolepment of plugins much more easier for me wink

    Greetings from germany,

    Sebastian
      • 11285
      • 6 Posts
      Ok, I studied create.php and update.php now for a couple of hours and after some "trial and error"-fun I finaly got it wink

      The Solution is not that simple, but a lot simpler than I thought:

      1. The right event handler for this is OnDocFormSave and not OnBeforeDocFormSave.
      OnBeforeDocFormSave works fine with new resources. But if we want to update an existing resource, there are some problems because the handling of TVs is totaly different then.

      So I channged the event handler from OnBeforeDocFormSave to OnDocFormSave and now my code looks like this:
            $custv = $modx->newObject('modTemplateVarResource');
            $custv->set('tmplvarid',4); // The id of my TV
            $custv->set('value',$teaserimage);  // The value I want for TV4     
            $custv->set('contentid',$id);  // The resource ID. We always have it when we use OnDocFormSave
            $custv->remove(); // Just a lazy thing for the moment. It's 3am here in germany and this is the easiest way for now to prevent redundant entries  
            $custv->save();
      


      That’s it! I testet it with new and existing resources.

      Hope it will help some others who are looking for the same problem.

      Late Greetings from germany,

      Sebastian
        • 3749
        • 24,544 Posts
        Good sleuthing. You could also do it with less code like this:
              $custv = $modx->getObject('modTemplateVar', 4);
              $custv->setValue ($id, $teaserimage);  // Resource ID, and the value I want for TV4     
              $custv->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
          • 38168
          • 14 Posts
          How can we do the same in Revo? Preview doesn't show and need refresh page, while plugin updates the field with $resource->setTVValue($TvValue,$img); OnDocFormSave