We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19599
    • 33 Posts
    Hi there,

    For my first attempt at writting a custom template variable I need to use my own html markup and js for the manager front-end. Now I'm stuck with how to save the contents from my custom template variable to the modx_site_tmplvar_contentvalues table.
    Unfortunately I can't find examples anywhere. Can anybody help me out?

    This question has been answered by minauen. See the first response.

      • 3749
      • 24,544 Posts
      You can have your JS make an Ajax call to a processor with code something like this (off the top of my head):

      <?php
      /* You need to set these from the data sent in the Ajax call */
      // $tvId = 
      // $resourceId = 
      // $value = 
      
      
      /** @var $tvr modTemplateVarResource */
      $fields = array(
          'tmplvarid' => $tvId,
          'contentid' => $resourceId,
      );
      $tvr = $modx->getObject('modTemplateVarResource', $fields);
      
      if (! $tvr) {
         $tvr = $modx->newObject('modTemplateVarResource');
         $tvr->set('tmplvarid', $tvId);
         $tvr->set('contentid', $resourceId);
      }
      
      $tvr->set('value', $value);
      $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
        • 19599
        • 33 Posts
        Hi BobRay

        Would I need to create a model class for my tv, create a connector and a processor that I would then call with my ajax call?
        Or would you just instantiate modx in my processor.php file?
          • 3749
          • 24,544 Posts
          Not unless a custom DB table is involved. See this for info on instantiating MODX http://bobsguides.com/blog.html/2013/07/13/using-modx-outside-of-modx/

          You probably wouldn't need the user and resource parts of the code since you're just creating and saving a modTemplateVarResource object.

          You will probably want to create a namespace so MODX will know where to find the processor and you'll need to put the processor in the right place.

          You don't have to, but you might consider using the new class-based processors. They're pretty slick and I think your case is very simple. https://www.markhamstra.com/xpdo/2012/getting-started-with-class-based-processors-2.2/
            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
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            If you make your AJAX processor a snippet in a resource with no template, you won't have to worry about connections and all that.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 4172
              • 5,888 Posts
              Quote from: minauen at Jan 31, 2014, 09:39 AM
              Hi there,

              For my first attempt at writting a custom template variable I need to use my own html markup and js for the manager front-end. Now I'm stuck with how to save the contents from my custom template variable to the modx_site_tmplvar_contentvalues table.
              Unfortunately I can't find examples anywhere. Can anybody help me out?

              what exactly should this TV do?
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 19599
                • 33 Posts
                Quote from: Bruno17 at Feb 03, 2014, 01:55 AM
                Quote from: minauen at Jan 31, 2014, 09:39 AM
                Hi there,

                For my first attempt at writting a custom template variable I need to use my own html markup and js for the manager front-end. Now I'm stuck with how to save the contents from my custom template variable to the modx_site_tmplvar_contentvalues table.
                Unfortunately I can't find examples anywhere. Can anybody help me out?

                what exactly should this TV do?

                Hi Bruno17

                I'm writting a simple TV to create image slideshows using a drag and drop UI. For the drag and drop capability I use the dropzone.js library. The data in the dropzone container should then be saved as a regular resource TV.
                  • 4172
                  • 5,888 Posts
                  something like this is allready possible with MIGX.
                  See this tutorial:
                  http://rtfm.modx.com/extras/revo/migx/migx.tutorials/migx.use-resource-specific-mediasource-and-multifile-uploader

                  This is loading a multifile-uploader, with drag/drop capability in a window with an iframe.
                  With a second button you can load the uploaded images into the MIGX-grid and you can sort them by drag/drop, activate/deactivate them, give them titles and descriptions.
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 19599
                    • 33 Posts
                    Quote from: Bruno17 at Feb 03, 2014, 03:10 AM
                    something like this is allready possible with MIGX.
                    See this tutorial:
                    http://rtfm.modx.com/extras/revo/migx/migx.tutorials/migx.use-resource-specific-mediasource-and-multifile-uploader

                    This is loading a multifile-uploader, with drag/drop capability in a window with an iframe.
                    With a second button you can load the uploaded images into the MIGX-grid and you can sort them by drag/drop, activate/deactivate them, give them titles and descriptions.


                    Hey Bruno,

                    Thanks for the input. I will give it a try. However, this is a good exercise to get to know MODX a little better ;-)
                      • 19599
                      • 33 Posts
                      Quote from: BobRay at Feb 02, 2014, 03:41 PM
                      Not unless a custom DB table is involved. See this for info on instantiating MODX http://bobsguides.com/blog.html/2013/07/13/using-modx-outside-of-modx/

                      You probably wouldn't need the user and resource parts of the code since you're just creating and saving a modTemplateVarResource object.

                      You will probably want to create a namespace so MODX will know where to find the processor and you'll need to put the processor in the right place.

                      You don't have to, but you might consider using the new class-based processors. They're pretty slick and I think your case is very simple. https://www.markhamstra.com/xpdo/2012/getting-started-with-class-based-processors-2.2/

                      Hi Bob,

                      I followed these things and managed to get it to work just fine. However there is one last issue I can't get to: My Custom Upload TV now saves directly upon successfully adding the files. If the user now hits Save on the top of the page, my TV of course appears empty and NULL is written to the database. How do I either pass the proper data format to the request, that saves the page or how can I get ignored, so my already saved TV values are not overwritten?