XF 1.1 Sort custom profile dropdown alphabetically

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 :)
 
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".
 
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.
 
In Model/UserField.php you could add the following above line 232:
natsort($choices);

So it looks like this:

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.

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.
 
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.
 
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.
 
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.

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.
 
This appears to work well:

library/XenForo/DataWriter/UserField.php

Add the red code:

Rich (BB 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.
 
Top Bottom