We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 52064
    • 120 Posts
    you can set up a TV so that it has a default value based on the associated template?

    thanks.

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

      FerX - Developer at Eracom s.r.l.
    • discuss.answer
      • 4172
      • 5,888 Posts
      I think, that's possible with form-customization-rules
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 52064
        • 120 Posts
        This is powerful.

        thanks.
          FerX - Developer at Eracom s.r.l.
          • 3749
          • 24,544 Posts
          You can also have the default value inherited from the closest ancestor that has the TV value set by setting the TV's default value to @INHERIT.

          You can set a default value to use of no ancestor has the TV set with something like @INHERIT blue.

          Doing this will let all descendants of a particular Resource inherit its TV value. As you might expect, this isn't nearly as fast as just setting all the TV values. Form Customization is also somewhat slow.

          A more efficient way would be not use a TV for that value at all, but to use a snippet to calculate the value on the fly based on the other TV.

          $referenceTvId-`12`; /* ID of the "other" TV */
          $value = $modx->resource->getTVValue($referenceTvId);
          
          $output = '';
          switch ($value) {
             case 'something':
                $output = 'option 1';
                break;
             case 'something else':
                $output = 'option 2';
                break;
           
             default:
                $output = 'Default Value';
                break;
          }
          
          return $output;


          For some kinds of logic, the switch doesn't work and you need to use a series of if statements:

          $referenceTvId-`12`; /* ID of the "other" TV */
          $value = $modx->resource->getTVValue($referenceTvId);
          
          $output = '';
          if ($value < 10) {
                $output = 'option 1';
          } elseif ($value >= 10 && $value <=20) {
                $output = 'option 2';
          } else {
                $output = 'Default Value';
          }
          
          return $output;



            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
            • 52064
            • 120 Posts
            Thanks for your suggestions.
            It would be very helpful if you could set directly in the template editing, TV tab.

            'I think it is not very difficult to implement'

            good day
              FerX - Developer at Eracom s.r.l.
              • 3749
              • 24,544 Posts
              That's a great idea. You could make a feature request here: https://github.com/modxcms/revolution/issues.

              One of the reasons I like to avoid using TVs to store data is that TVs are very slow and inefficient. When a TV is set to its default value, its value is not stored in the DB table (there's no record at all for that resource's TV value).

              For every TV, MODX has to check that table to look for a value for the current resource. If the value is set to @INHERIT, MODX has to get all the values for all the resources going up the chain until it finds one with it set. If it doesn't find one, it has to get the TV object and then get its default value.

              If the DB value for that resource is missing, MODX has to get the TV object and then get its default value.

                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