We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17472
    • 5 Posts
    Hey, I am quite new to MODx and I’ve stumbled upon retrieving allowed values of a TV for a filter (I have defined a list of cities in TV and I would like to retrieve it, without the need for duplicate the list and mantain it in two places). How to achieve it?

    Regards.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Which version of MODx?

        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
        • 17472
        • 5 Posts
        Newest Revolution.
          • 33968
          • 863 Posts
          Hi, not sure exactly what you are trying to achieve here. But as I learned only this morning you could create a system setting with a comma separated list of cities, then pull it into your TV, a snippet, or directly into your template if you wish. You’d only have to maintain the one list and that would be my recommendation, but it might not be ideal if you don’t want the end user to access the site settings.

          Otherwise you could use an @select binding to grab your existing TV’s settings direct from the database. Kinda depends on what you need to achieve...
            • 17472
            • 5 Posts
            Well, I solved the problem myself. In case if someone needs this, take a look. Allowed values have to come in form ’value1||value2||value3’. I know it could be more elegant, but the following snippet works fine for me now.

            <?php
            global $modx;
            $output = '';
            
            $createSelectForm =  (isset($createSelectForm)) ? true : false;
            $property = (isset($property)) ? $property : 'DEFAULT';
            $defaultPosition = (isset($defaultPosition)) ? $defaultPosition : 'DEFAULT';
            
            $result = $modx->db->select("elements", $modx->getFullTableName("site_tmplvars"), "name='" . $property . "'");
            
            if($modx->db->getRecordCount($result))
            {
                 $row = $modx->db->getRow($result);
                 $resultString = $row['elements'];
            
                 $propertiesArray = explode("||", $resultString);
            
                 if ($createSelectForm) {
                    // creating <option>entries</option>
            
                    $output .= "<option value=''>" . $defaultPosition . "</option>";
                    foreach ($propertiesArray as $property) {
                        $output .= '<option>' . $property . '</option>';
                    }
                 } else {
                    // creating plain list
                    foreach ($propertiesArray as $property) {
                        $output .= $property . ', ';
                    }
            
                 }
            }
            else
            {
                 $output .= 'EMPTY';
            }
            
            return $output;
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              You’ve basically got the right idea; I’m sure though that Revo has an API function for returning the default value of a given TV.
                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