We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • I figured out a way to use drop-down menus for extended fields in Login.UpdateProfile. It’s clunky, so I’m wondering; is there a better way? If not, hopefully this will be useful for someone else. Maybe worth documenting on RTFM?

    Basically, I combo it with the If snippet to do a comparison to the field’s value, and add ’selected="selected"’ if it matches, like so:

    <select name="height">
    	<option value="[[!If? &subject=`[[+height]]` &operator=`EQ` &operand=`65` &then=`[[+height]]" selected="selected` &else=`65`]]">6'5" or taller</option>
    	<option value="[[!If? &subject=`[[+height]]` &operator=`EQ` &operand=`64` &then=`[[+height]]" selected="selected` &else=`64`]]">6'4"</option>
    	<option value="[[!If? &subject=`[[+height]]` &operator=`EQ` &operand=`63` &then=`[[+height]]" selected="selected` &else=`63`]]">6'3"</option>
    	<option value="[[!If? &subject=`[[+height]]` &operator=`EQ` &operand=`62` &then=`[[+height]]" selected="selected` &else=`62`]]">6'2"</option>
    	<option value="[[!If? &subject=`[[+height]]` &operator=`EQ` &operand=`61` &then=`[[+height]]" selected="selected` &else=`61`]]">6'1"</option>
    	<option value="[[!If? &subject=`[[+height]]` &operator=`EQ` &operand=`60` &then=`[[+height]]" selected="selected` &else=`60`]]">6'0"</option>
    	<option value="[[!If? &subject=`[[+height]]` &operator=`EQ` &operand=`511` &then=`[[+height]]" selected="selected` &else=`511`]]">5'11"</option>
    	<option value="[[!If? &subject=`[[+height]]` &operator=`EQ` &operand=`510` &then=`[[+height]]" selected="selected` &else=`510`]]">5'10"</option>
    	<option value="[[!If? &subject=`[[+height]]` &operator=`EQ` &operand=`59` &then=`[[+height]]" selected="selected` &else=`59`]]">5'9"</option>
    	<option value="[[!If? &subject=`[[+height]]` &operator=`EQ` &operand=`58` &then=`[[+height]]" selected="selected` &else=`58`]]">5'8"</option>
    	<option value="[[!If? &subject=`[[+height]]` &operator=`EQ` &operand=`57` &then=`[[+height]]" selected="selected` &else=`57`]]">5'7"</option>
    	<option value="[[!If? &subject=`[[+height]]` &operator=`EQ` &operand=`56` &then=`[[+height]]" selected="selected` &else=`56`]]">5'6"</option>
    	<option value="[[!If? &subject=`[[+height]]` &operator=`EQ` &operand=`55` &then=`[[+height]]" selected="selected` &else=`55`]]">5'5" or shorter</option>
    </select>
      • 22427
      • 793 Posts
      Because the value of [tt]&else[/tt] is always identical to that of [tt][[+height]][/tt] in the case the condition is true, you could simplify it in this way:
      <option value="65 [[!If? &subject=`[[+height]]` &operator=`EQ` &operand=`65` &then=` selected="selected`]]">6'5" or taller</option>

        • 47633
        • 11 Posts
        An old thread I know, but I am in a similar situation.

        I have 5 drop down boxes that are used to set user preferences e.g.

        <select class="select" name="Sector" id="Sector"> 
        	<option value="Agencies & Sponsors">Agencies & Sponsors</option>
        	<option value="Clubs & Teams">Clubs & Teams</option>
        	<option value="Education & Charities">Education & Charities</option>
        </select>
        


        Sector is an extended field that is set for a user, all I want to do is have the value they've selected previously (that part works fine), be selected when they visit the page.

        I've thought about the option above, or writing a snippet along the lines of:
        [[!isItSelected? &key=`Sector` &value=`Agencies & Sponsors`]]


        I've written something below just as an idea thich would return 'selected' if the users Sector extended field was 'Agencies & Sponsors'. But even that seems a bit messy.

        $output="";
        $itemKey = $scriptProperties['key'];
        $itemValue = $scriptProperties['value'];
        
        
        $userId = $modx->getOption('userId',$scriptProperties,false);
        $user = $modx->getObject('modUser',$userId);
        $profile = $user->getOne('Profile');
        
        // Get extended profile
        $extended = $profile->get('extended');
        
        if ($extended[$itemKey] == $itemValue){
        	$output = "selected";
        }
        
        return $output;
        
          • 3749
          • 24,544 Posts
          That looks about right to me, though I'd suggest adding some sanity checks so things don't crash if the user has no Profile (e.g., the (anonymous) user), the key or value are not set, or the extended field is not set -- something like this:


          $output="";
          $itemKey = $modx->getOption('key', $scriptProperties, null);
          $itemValue = $modx->getOption('value', $scriptProperties, null);
           
          if ($itemKey == null || $itemValue == null) {
              return;
          } 
          
          $userId = $modx->getOption('userId',$scriptProperties,false);
          $user = $modx->getObject('modUser',$userId);
          $profile = $user->getOne('Profile');
           
          // Get extended profile
          if ($profile) {
              $extended = $profile->get('extended');
              if ($modx->getOption($itemKey, $extended) == $itemValue) {
                  $output = 'selected="selected"';
              }
          
          }
           
          return $output;


          You might also want to consider extending the modUser object with ClassExtender and storing the preferences in a custom table rather than the extended field of the Profile. That would open up a lot of options for very fast searching and sorting.

          http://bobsguides.com/classextender-class.html
            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
            • 47633
            • 11 Posts
            Thanks for the reply - Just got back to this after the weekend.
            I went down this route, but the snippet was never returning selected="selected". On closer inspection I realised that the snippet was never getting the logged in user.
            These lines didn't work:
            $userId = $modx->getOption('userId',$scriptProperties,false);
            $user = $modx->getObject('modUser',$userId);
            


            So I replaced it with :
            $user = $modx->user;


            Which does work and the drop down fields are now selected as per the users extended field.

            I'm just confused as to why the code you (and I) wrote didn't work, and why those two lines would be used in favour of the shorter, one line.
              • 3749
              • 24,544 Posts
              My bad. I didn't realize that you wanted the current user and assumed that you were sending the ID of the user you wanted as an &id property in the tag.

              This would be the method for the current user:

              $id = $modx->user->get('id');


              If the user is not logged in, the ID will be 0.
                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