We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8522
    • 145 Posts
    Hi, How can I populate a select box / dropdown with a list of all countries?

    I’d like to use the modx internal list of countries starting with Afghanistan, exactly the same as under Security / Manage Users / Update User / Country

    If validation has failed it would make sense that the form remembers which country has been selected, so I’d like to use :FormItIsSelected. At this point I guess I’ll need to write a custom snippet that generates

    <option value="Afghanistan" [[!+fi.countries:FormItIsSelected=`Afghanistan`]]>Afghanistan</option>


    Any pointers appreciated!
      • 22015
      • 3 Posts
      Ok as eerne knows on IRC I was inspired to create a fast snippet with some code borrowed from MODX donated back to MODX wink

      First the snippet and name it CountryList for example:

      <?php
      $clRowTpl = isset( $clRowTpl ) ? $clRowTpl : 'clRowTpl';
      $clOuterTpl = isset( $clOuterTpl ) ? $clOuterTpl : 'clOuterTpl';
      
      $_country_lang = array();
      include $modx->getOption('core_path').'lexicon/country/en.inc.php';
      if ($modx->getOption('manager_language') != 'en' && file_exists($modx->getOption('core_path').'lexicon/country/'.$modx->getOption('manager_language').'.inc.php')) {
          include_once $modx->getOption('core_path').'lexicon/country/'.$modx->getOption('manager_language').'.inc.php';
      }
      asort($_country_lang);
      
      $countries = array();
      $wrapper = "";
        foreach ($_country_lang as $country) {
          $modx->setPlaceholder('cl.country', $country);
          $wrapper .= $modx->getChunk($clRowTpl);
      }
      $modx->setPlaceholder('cl.wrapper', $wrapper);
      $output = $modx->getChunk($clOuterTpl);
      
      return $output;


      Create 2 chunks:
      clRowTpl for the <option> area like: <option>[[+cl.country]]</option>
      clOuterTpl for the <select area like: <select>[[+cl.wrapper]]</select>

      This snippet and the rest was made on demand so no testing has been done or funstuff. I might do a second version if needed to be able to contrl more stuff like where to place the selected option and add a first description select.

      In this version you can call it from a resource like [[CountryList? &clRowTpl=`row` &clOuterTpl=`outer`]]
        • 8522
        • 145 Posts
        Thanks so much octoris! even with :FormItIsSelected it works all nicelly, here my chunks:
        <select name="country"[[!+fi.country:FormItIsSelected=`none`:is=`selected="selected"`:then=` class="invalid"`]]>
          <option value="none">Please select</option>
          [[+cl.wrapper]]
        </select>

          <option value="[[+cl.country]]" [[!+fi.country:FormItIsSelected=`[[+cl.country]]`]]>[[+cl.country]]</option>
        

        thanks a lot!
          • 20135
          • 188 Posts
          Quick edit to the code above - I wanted to add a default "selected" option.

          Snippet Code:
          <?php
          $clRowTpl = isset( $clRowTpl ) ? $clRowTpl : 'clRowTpl';
          $clOuterTpl = isset( $clOuterTpl ) ? $clOuterTpl : 'clOuterTpl';
          // added this option
          $clSelected = isset( $clSelected ) ? $clSelected : '';
           
          $_country_lang = array();
          include $modx->getOption('core_path').'lexicon/country/en.inc.php';
          if ($modx->getOption('manager_language') != 'en' && file_exists($modx->getOption('core_path').'lexicon/country/'.$modx->getOption('manager_language').'.inc.php')) {
              include_once $modx->getOption('core_path').'lexicon/country/'.$modx->getOption('manager_language').'.inc.php';
          }
          asort($_country_lang);
           
          $wrapper = "";
          foreach ($_country_lang as $country) {
            	// added this if statement
            	if ($clSelected === $country) {
            		$modx->setPlaceholder('cl.selected', 'selected'); 	
            	} else {
            		$modx->setPlaceholder('cl.selected', '');
            	}
              $modx->setPlaceholder('cl.country', $country);
              $wrapper .= $modx->getChunk($clRowTpl);
          }
          $modx->setPlaceholder('cl.wrapper', $wrapper);
          $output = $modx->getChunk($clOuterTpl);
           
          return $output;

          And then for the row tpl:
          <option [[+cl.selected]]>[[+cl.country]]</option>

          Now just add the name of your chosen country:
          [[CountryList? &clRowTpl=`row` &clOuterTpl=`outer`  &clSelected=`Australia`]]

          Easy! Great snippet eerne!
            • 29059
            • 88 Posts
            hi all,

            i keep trying everything above but all it does is break my page.

            what am i doing wrong?

            FYI 1:
            in an effort to provide all the relevant info, here's what i'm using & doing...

            ...EDITED OUT THE REST DUE TO FINDING THE ANSWER...

            I FOUND THE SOLUTION (for me) @ https://rtfm.modx.com/extras/revo/login/login.tutorials/login.extended-user-profiles#Login.ExtendedUserProfiles-Variations

            SCROLL TO THE VERY BOTTOM OF THE PAGE, TO THE "VARIATIONS" SECTION.
            IT WORKS OUTTA-THE-BOX, BUT YOU DO HAVE TO INSTALL FORMIT FOR IT TO WORK. BUT THAT'S IT. YOU DON'T HAVE TO DO ANYTHING ELSE WITH FORMIT IF YOU DON'T WANT.
            -2¢ [ed. note: syberknight last edited this post 8 years, 2 months ago.]