We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1209
    • 49 Posts
    I want to make some connection between the TV and the document variables - to change the docvars right before saving the document using the TVs.

    But I can’t get the TVs available in the event [tt]OnBeforeDocFormSave[/tt], where I can change the docvars.

    OnBeforeDocFormSave:
    <?php
    global $tmplvars;
    
    print_r ($tmplvars);
    
    $modx->messageQuit('Debug stopping');
    


    This plugin gives me [tt]Array ( )[/tt] as a [tt]print_r[/tt] result.

    That means that [tt]$tmplvars[/tt] is already defined as Array, which happens in [tt]save_content.processor.php #130[/tt], but has no elements inside, though they are perfectly collected in lines 173-181. And calls to OnBeforeDocFormSave happen only at #235 for the new document and #416 for the updated.

    Where do the contents of [tt]$tmplvars[/tt] disappear?
      • 18573
      • 30 Posts
      As I think $tmpvars are already cleaned when you try to display it.

      This block are work in 1.0.5

      <?
      
      global $tmplvars;
      $e = &$modx->Event;
      if ($e->name == 'OnBeforeDocFormSave'){
      print_r ($tmplvars);
      $modx->messageQuit('Debug stopping');
      }
      ?>


      Output
      Array
      (
          [22] => Array
              (
                  [0] => 22
                  [1] => запонка
              )
      
          [15] => Array
              (
                  [0] => 15
                  [1] => 01.01
              )
      
          [1] => Array
              (
                  [0] => 1
                  [1] => 7410
              )
      
          [18] => Array
              (
                  [0] => 18
                  [1] => есть
              )
      
          [promo] => 16
          [17] => Array
              (
                  [0] => 17
                  [1] => 17,46 г
              )
      
          [19] => Array
              (
                  [0] => 19
                  [1] => assets/images/collections/nature/ladybug/01.01/IMG_01_c.jpg
              )
      
          [21] => Array
              (
                  [0] => 21
                  [1] => assets/images/collections/nature/ladybug/01.01/IMG_01-p.png
              )
      
          [9] => Array
              (
                  [0] => 9
                  [1] => серебро
              )
      
          [3] => Array
              (
                  [0] => 3
                  [1] => золото
              )
      
          [2] => Array
              (
                  [0] => 2
                  [1] => коралл||кристаллы Сваровски
              )
      
          [4] => Array
              (
                  [0] => 4
                  [1] => [["assets/images/collections/nature/ladybug/01.01/IMG_01.jpg",""],["assets/images/collections/nature/ladybug/01.01/IMG_02.jpg",""],["assets/images/collections/nature/ladybug/01.01/IMG_03.jpg",""],["assets/images/collections/nature/ladybug/01.01/IMG_04.jpg",""],["assets/images/collections/nature/ladybug/01.01/IMG_05.jpg",""]]
              )
      
      )


      For manual writing to current page TV use
      $tmplvars[$TVID][0] = $TVID;
      $tmplvars[$TVID][1] = 'TV value post here';
      


      // I know that this topic is old but I can’t find correct info about this problem, so better if this reply google will save
        • 3749
        • 24,544 Posts
        If you're saving an existing doc, the ID should be available as $id in OnBeforeDocFormSave.

        $tvValue = $resource->getTVValue($tvNameOrID);
        $resource->set('someField', $tvValue);
        


        If it's not available that way, put this at the top:

        $id = $resource->get('id');


        Because of the way TVs are stored, I'm not sure the $tmplvars array is reliable, and anyway, it could disappear or be changed in future versions. getTVValue() should always work.

        If it's a new doc, it won't have any TV values.
          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