We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20380
    • 118 Posts
    I have a plugin:

    switch ($modx->event->name) {
    	case 'OnDocFormRender':
      		$resource->set("pagetitle", "111");
      		$resource->set("tv4", "!!!");
      	break;
    	case 'OnDocFormPreRender':
      		$resource->set("longtitle", "111");
      		$resource->set("tv4", "!!!");
      	break;
    }


    I need to change value of tv dinamic, when form loading.
    $resource->set("pagetitle", "111"); - work (OnDocFormRender), but $resource->set("tv4", "!!!"); - did’t. How to set it?
      • 3749
      • 24,544 Posts
      TV values for resources are not stored with the resource, but in a different table.

      I think this would do it (assuming that 4 is the ID of the TV):

      <?php
      /* get the tvr for this resource */
      $tvr = $modx->getObject('modTemplateVarResource', array ('tmplvarid'=>'4', 'contentid'=>$resource->get('id') );
      
      /* create tvr if it doesn't exist yet */
      if (empty ($tvr)) {
          $tvr = $modx->newObject('modTemplateVarResource', array(('tmplvarid'=>'4', 'contentid'=>$resource->get('id') );
      }
      /* set the value and save */
      $tvr->set('value', 'whatever');
      $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
        • 20380
        • 118 Posts
        You did’t understand me. Don’t need change tv in db. I need change it only when open Resource -> Create\Edit (only wiev). Resource variables I could change in $resource->set("pagetitle", "111"); How can I change tv-variable?
          • 3749
          • 24,544 Posts
          I understood you, but the Create/Edit Resource panel is going to show the value for that TV that’s in the DB (or the default value if no value is set). You can change the fields of the resource object, because MODX creates a handle to it before rendering the form. There’s no such thing (that I know of) for TVs.

          You can change the default value of the TV in the form on the Create/Edit Resource panel with a Form Customization rule and use a constraint to limit the rule to a certain resource if that will meet your needs.

          You could also change the default value for the TV itself if that would work.
            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