Snippet: Order CCK field's allowed values depending on language
Last modified: September 24, 2009 - 21:39
Goal: With a CCK Text field using select list (radio buttons) widget, change order of allowed values depending on current language.
Example: When creating a profile, users have to select their preferred language for receiving mails. The list is static as it only contains two languages but should change the order related to current interface language. So if users use Catalan language the see this language as first option but when they use Spanish the order is reverse.
How to do it
- Make the field. Go to the CCK node type's edit form, 'manage fields' tab.
- Create a node of type 'text', with a 'select list' widget.
- Click 'configure' next to the field entry. This opens an edit form just for that field.
- Check "Required" and "Number of values" 1
- Near the bottom is the 'allowed values' fieldset. Within that fieldset is an area named 'PHP code'.
**To access this, you must be using an account with the Content Module "Use PHP input for field settings (dangerous - grant with care)" permission. - Next, you want a variation on the following:
$current_lang = i18n_get_lang();
$allowed_values = array('catala' => t('Català'), 'espanyol' => t('Espanyol'));
if ($current_lang == 'es') $allowed_values = array_reverse($allowed_values, true);
return $allowed_values;