Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

For example if a CCK text field is configured to have the following allowed values:

US|United States
UK|United Kingdom

The node has the corresponding field:

node->country

and its value can be found by accessing:

node->country[0]['value']

which will return 'US' or 'UK'. What is best way to return the display name which was specified in the allowed list (i.e "United States" or "United Kingdom")?

share|improve this question

2 Answers 2

You can use the following code.

$field = content_fields($field_name);
$function = $field['module'] . '_allowed_values';
$values = array_values(function_exists($function) ? $function($field) : (array) content_allowed_values($field));

This code is similar to the code used by optionwidgets_options(); the difference is that optionwidgets_options() returns also the value for the empty choice.

The output of content_allowed_values($field) is an array that in your case is the following one.

array(
  'US' => 'United States',
  'UK' => 'United Kingdom',
);

As you are only interested to the options visible in the user interface, I passed the result of content_allowed_values($field) to array_values().

share|improve this answer

Install the devel module and go to a node with this field and click the devel tab. If its available directly it will show in devel. If not you may have to do a custom db query.

share|improve this answer
    
It's not available. I thought there was an API for that? –  haggag Jun 13 '12 at 16:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.