We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39501
    • 163 Posts
    Would it be possible to assign a TV to a particular template based on the output of a Yes/No TV? Either in ClientConfig Add-on or regular yes/no TV. [ed. note: jonleverrier last edited this post 11 years, 2 months ago.]
      • 40045
      • 534 Posts
      Totally untested, but maybe with a plugin OnDocFormPrerender...it would check the value of the yes/no tv before a manager/resource form is rendered and if yes it could either assign the tv to the template or change the type from "hidden" to whatever type it really should be when it's editable.
        • 39501
        • 163 Posts
        @exside - Thanks for the post. I wonder if an add-on exists for something like this already.
          • 3749
          • 24,544 Posts

          I don't think there is one, but the code would look something like this (untested).

          I would put the code in a plugin attached to OnDocFormSave.


          <?php
          $yesNoTvId = 12; // ID of Yes/No TV
          $tvId = 14; // id of TV to attach
          $templateId = 3; // ID of Template to attach it to
          
          
          /* get the templateVarTemplate if it exists */
             $tvr = $modx->getObject('modTemplateVarTemplate', array('tmplvarid' => $tvId, 'templateid' => $templateId));
          
          /* check the YesNo TV */
          $attach = $resource->getTVValue($yesNoTvId);
          
          if ($attach) { // yes
              
          
             /* create it if necessary */
             if (!$tvr) {
                 $tvr = $modx->newObject('modTemplateVarTemplate');
             }
          
             /* set its values and save */
             $tvr->set('tmplvarid', $tvId);
             $tvr->set('templateid', $templateId);
             $tvr->save();
          } else {  // no
             /* if templateVarTemplate exists and YesNo TV is set to no, remove the connection */
             if ($tvr) {
                $tvr->remove;
             }
          }
          [ed. note: BobRay last edited this post 11 years, 2 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
            • 39501
            • 163 Posts
            @bobray - thanks for the code! had a play with it this morning...

            When you select yes, the TV displays. When you select no, the TV is still there. Any ideas?
              • 3749
              • 24,544 Posts
              I'm not sure what you mean by "the TV is still there," but the code doesn't delete the TV, just the modTemplateVarTemplate, which means that the TV is no longer connected to that template. That may not take effect until the cache is cleared.

              Attaching the plugin to OnBeforeDocFormSave instead of OnDocFormSave might make it take effect immediately.
                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
                • 39501
                • 163 Posts
                So when I click "yes" the TV is given access to my specified template and appears.

                When I select "no" from my yes/no TV, the TV specified in the plugin is still connected to the template. I've tried loading the plugin via OnBeforeDocFormSave, OnDocFormSave & clearing the cache, but the TV is still connected to my specified template.

                I'll have a play - appreciate your help Bobray! Legend as always.

                  • 3749
                  • 24,544 Posts
                  It shouldn't happen, but it's possible that there is more than one modTemplateVarTemplate connecting the two, so when you remove one, the other is still there.

                  Also, double check the spellings of the two field values in both places and make sure they're in all lowercase. It's easy to get them wrong, especially the first one.

                  tmplvarid
                  templateid

                  Of course it could be something wrong with my code, since it's untested. Maybe someone else will notice a mistake.
                    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
                    • 39501
                    • 163 Posts
                    Cheers Bob!