We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17249 ☆ A M B ☆
    • 165 Posts
    Is it possible to use a Template Variable as a helper for rendering output of a custom Object's field value?
    I have a select drop-down TV with the name order_status with the following input options:

    Waiting for Payment==WAITING||Payment Received==PAYMENT_RECEIVED||Shipped=SHIPPED


    Then i have an Order custom Object where i can do something like this:

    
    $order = $modx->getObject('customOrder',33);
    $status = $order->get('order_status');
    
    
    // Here's where i need to call the template variable template
    // to help me render "Waiting for Payment" instead of the original field value "WAITING"
    
    $renderedStatus = "?????"
    
    $ouput .= $renderedStatus
    



    Remember i'm using a custom object here and the normal getTVValue will not help as i'm not using modResource as the source of the data.
    Any suggestions?
    Regards,

    Pablo. [ed. note: lithiumlab last edited this post 12 years, 7 months ago.]
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      If this orderObject is associated with the current resource, or if it is associated with a known resource, then you can just get the value of the TV for that resource.
      http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/template-variables/accessing-template-variable-values-via-the-api
        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
        • 17249 ☆ A M B ☆
        • 165 Posts
        Thanks for the reply.
        I've already checked the docs, and the examples are related to modResource

        Order objects in my case are not related to modResource.
        I'm using a MIGXdb / custom package to render the update form in the backend. (See screenshot attached)
        A helper Single Select type TV called order_status renders the input options in the dropdown.

        Now i want an easy, reusable way to render the friendly value in the frontend using the helper TV.
        Its applicable to many other uses cases i think.

        Is it possible to process a value trough the template variable without sending a modResource reference?
        Tha same way i'm using a helper TV to render input options i want it to help render output in the frontend.
        Maybe i'm missing something obvious smiley


        [ed. note: lithiumlab last edited this post 12 years, 6 months ago.]
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Where is the TV value coming from? Is it a default value when you create the TV, or is the value coming from a resource - for example, if a product is a resource.
            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
            what are the input-option-values of that TV?

            [Edit]
            sorry, didn't read the whole thread.

            found it

            Waiting for Payment==WAITING||Payment Received==PAYMENT_RECEIVED||Shipped=SHIPPED


            ok, hardcoded, then its easy to get them, will post the solution later today
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 17249 ☆ A M B ☆
              • 165 Posts
              Not using resources. This is a custom Object to be manipulated with a migxdb form.
              I'm using the tv only as helper to render input options in the update form. It works as expected in the manager.

              Orders are created programatically when a user checksout in the frontend.
              a WAITING status is assigned to that order at creation time until the order is confirmed using PayPal IPN Message to change it to PAYMENT_RECEIVED

              So i end up with a custom_order table with a order_status field where this is stored.
              In the frontend i want to display the user's order history with cart items and status.

              If i do something like
              [[+order_status]]


              the result will be "WAITING" instead of "Waiting for Payment"
              I just feel the need of a reusable way of using TV as templates for outputting this data. (which is not linked to Resources)
              An output filter maybe will suffice if i can extract input options from a given Template Variable called by its name.
              Something like:

              [[+order_status:renderWith=`helperTVname`]]


              I can't find how to do it in the docs.

              Quote from: sottwell at Mar 25, 2014, 09:58 AM
              Where is the TV value coming from? Is it a default value when you create the TV, or is the value coming from a resource - for example, if a product is a resource.
                • 17249 ☆ A M B ☆
                • 165 Posts
                Thanks Bruno;
                Will patiently await for it.



                Quote from: Bruno17 at Mar 25, 2014, 10:21 AM
                what are the input-option-values of that TV?

                [Edit]
                sorry, didn't read the whole thread.

                found it

                Waiting for Payment==WAITING||Payment Received==PAYMENT_RECEIVED||Shipped=SHIPPED


                ok, hardcoded, then its easy to get them, will post the solution later today
                  • 4172
                  • 5,888 Posts
                  try this one. This should work with hardcoded inputOptionValues and with dynamic generated options by Bindings

                  //[[+status:optionstextoftv=`tvname`]]
                  
                  $tvname = $options;
                  
                  if (!empty($tvname) && $tv = $modx->getObject('modTemplateVar', array('name' => $tvname))) {
                      $elements = $tv->parseInputOptions($tv->processBindings($tv->get('elements')));
                      //$elements = explode('||', $tv->get('elements'));
                      foreach ($elements as $element) {
                          $opt = explode('==', $element);
                          $value = count($opt) > 1 ? $opt[1] : $opt[0];
                          $text = $opt[0];
                          $text_array[$value] = $opt[0];
                      }
                  }
                  
                  return $text_array[$input];
                  
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 17249 ☆ A M B ☆
                    • 165 Posts
                    Terrific solution!
                    Thanks a lot Bruno and Susan;
                    Amazing community.

                    I'll test and post the results.
                    this is exactly what i needed.





                    Quote from: Bruno17 at Mar 25, 2014, 03:56 PM
                    try this one. This should work with hardcoded inputOptionValues and with dynamic generated options by Bindings

                    //[[+status:optionstextoftv=`tvname`]]
                    
                    $tvname = $options;
                    
                    if (!empty($tvname) && $tv = $modx->getObject('modTemplateVar', array('name' => $tvname))) {
                        $elements = $tv->parseInputOptions($tv->processBindings($tv->get('elements')));
                        //$elements = explode('||', $tv->get('elements'));
                        foreach ($elements as $element) {
                            $opt = explode('==', $element);
                            $value = count($opt) > 1 ? $opt[1] : $opt[0];
                            $text = $opt[0];
                            $text_array[$value] = $opt[0];
                        }
                    }
                    
                    return $text_array[$input];
                    
                      • 17249 ☆ A M B ☆
                      • 165 Posts
                      Worked like charm.
                      Thanks a lot.