1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

XF 1.1 Sort custom profile dropdown alphabetically

Discussion in 'XenForo Questions and Support' started by Russ, Jul 23, 2012.

  1. Russ

    Russ Well-Known Member

    Is there an easy way of having the dropdown menu from a custom profile field automatically sort the custom options alphabetically? I don't have many options currently so I'm just rearranging as I add new options but I can see this being a pain down the road.

    I'm referring to a dropdown menu created through custom profile fields. Thanks ahead of time for your time :)
     
  2. Naatan

    Naatan Well-Known Member

    You'd need to write an addon that extends all relevant controllers to do the sorting. It's not something that's natively supported.

    So no, there is no easy way. Although writing an addon for it wouldn't be particularly hard, I don't think that's what you are referring to with "easy".
     
  3. Russ

    Russ Well-Known Member

    Correct sir :), thanks for the response I'll post a request up
     
  4. Syndol

    Syndol Guest

    In Model/UserField.php you could add the following above line 232:
    natsort($choices);

    So it looks like this:
    PHP:
    if (!$master)
    {
        
    natsort($choices);
       
        foreach (
    $choices AS $value => &$text)
        {
            
    $text = new XenForo_Phrase($this->getUserFieldChoicePhraseName($fieldId$value));
        }
    }
    But that would still leave it unsorted (order in which you entered the values originally) in the Admin Control Panel.
     
    Trained Monkey likes this.
  5. Russ

    Russ Well-Known Member

    But in the actual drop down field on the profile it would be sorted? Also would I need to redo the edit with each upgrade?

    Thanks ahead of time Syndol, huge help for looking at this.
     
  6. Syndol

    Syndol Guest

    You will have to create an add-on to extend the getUserFieldChoices() function if you do not wish to reapply the change with every upgrade.
     
  7. Naatan

    Naatan Well-Known Member

    Please do yourself a favour and never hack XenForo code. The addon system is very elaborate and should in most situations suffice, it certainly does in this case.
     
  8. Did anyone do this? Because not sorting this list alpha-numerically makes it quite confusing if you ever need to add options (as I've just had to do) after the initial list is made.
     
  9. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    This appears to work well:

    library/XenForo/DataWriter/UserField.php

    Add the red code:

    Code:
    	/**
    	 * Sets the choices for this field.
    	 *
    	 * @param array $choices [choice key] => text
    	 */
    	public function setFieldChoices(array $choices)
    	{
    		foreach ($choices AS $value => &$text)
    		{
    			if ($value === '')
    			{
    				unset($choices[$value]);
    				continue;
    			}
    
    			$text = strval($text);
    
    			if ($text === '')
    			{
    				$this->error(new XenForo_Phrase('please_enter_text_for_each_choice'), 'field_choices');
    				return false;
    			}
    
    			if (preg_match('#[^a-z0-9_]#i', $value))
    			{
    				$this->error(new XenForo_Phrase('please_enter_an_id_using_only_alphanumeric'), 'field_choices');
    				return false;
    			}
    
    			if (strlen($value) > 25)
    			{
    				$this->error(new XenForo_Phrase('please_enter_value_using_x_characters_or_fewer', array('count' => 25)));
    				return false;
    			}
    		}
    
    		// SORT CHOICES ALPHABETICALLY
    		asort($choices);
    
    		$this->_fieldChoices = $choices;
    		$this->set('field_choices', $choices);
    
    		return true;
    	}
    
    That causes all choice options for "choice" fields to sort alphabetically when editing or saving the field in the Admin CP.

    This appears to work well in testing.
     
    Trained Monkey likes this.

Share This Page