We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33238
    • 388 Posts
    Quote from: donshakespeare at Aug 29, 2017, 05:38 PM
    If for 20+ TVs, just do a tiny loop, done!

    Mate, can I use the second option for more than one tv in one code?

    for example:

    <?php
    $tvId = 20;
    $tvId = 30;
    $tvId = 24;
    $defaultTVvalue = "";
    bla bla bla....


    or I need to do one plugin per TV field?

    thanks.
      --
      ysanmiguel.com
      • 42562
      • 1,145 Posts
      No, plugin per tv would be utterly tedious.

      Put all tv ids in an array (in system settings : 3, 5, 87, 65, 5, 6, 8, or directly in the one plugin.

      Then a simple PHP foreach.
      If you are still stuck, I can whip something up.
        TinymceWrapper: Complete back/frontend content solution.
        Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
        5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
        • 3749
        • 24,544 Posts
        I think the loop would be better inside the JS, rather than registering 20 blocks of JS code.

        $modx->regClientStartupHTMLBlock('
        <script>
           Ext.onReady(function() {
           arr = array('20','12','34');
        
           for (var i = 0, len = arr.length; i < len; i++) {
              // var tv = document.getElementById("tv'.$tvId.'");
              var tv = Ext.get("tv'.$arr[i].'");
              var tvDef = Ext.get("tvdef'.$arr[i].'");
         
              //Note upon save, this tv will have a real value in Database
              if(tv){
                  tvValue = tv.dom.value;
                  tv.dom.value = tvValue || "'.$defaultTVvalue.'";
              }
           }
         
            //If you have that squiggly thing for TV refresh, the value will be set
            if(tvDef){
              tvDefValue = tvDef.dom.value;
              tvDef.dom.value = tvDefValue ||  "'.$defaultTVvalue.'";
            }
           });
        </script>
        ');


        [ed. note: BobRay last edited this post 6 years, 7 months ago.]
          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
          • 42562
          • 1,145 Posts
          Yup that's it right there.
          Exactly my thought.

          You could even go crazy and get TV id from TV name.
          If TV names are more stable in your project.

          Even get that value from system settings, then explode the list in the plugin
            TinymceWrapper: Complete back/frontend content solution.
            Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
            5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
            • 3749
            • 24,544 Posts
            I find that it's difficult to get all the quotes right in a block like this. It might be a good case for PHP's heredoc format, which preserves quotation marks and interprets the variables, though some variables need curly braces.

            $script = <<< QUOTE
            <script>
               Ext.onReady(function() {
               arr = array('20','12','34');
               var tvValue;
               var tvDefValue;
             
               for (var i = 0, len = arr.length; i < len; i++) {
                  // var tv = document.getElementById("tv'.$tvId.'");
                  var tv = Ext.get("tv{arr[i]}");
                  var tvDef = Ext.get("tvdef{arr[i]}");
              
                  //Note upon save, this tv will have a real value in Database
                  if(tv){
                      tvValue = tv.dom.value;
                      tv.dom.value = tvValue || "$defaultTVvalue";
                  }
               }
              
                //If you have that squiggly thing for TV refresh, the value will be set
                if(tvDef){
                  tvDefValue = tvDef.dom.value;
                  tvDef.dom.value = tvDefValue ||  "$defaultTVvalue";
                }
               });
            </script>
            QUOTE;
            $modx->regClientStartupHTMLBlock($script);
              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