3

Given the following select list for the "field_priority" field, how can I display the label, given the key (e.g. 0, 1, 3)?

0|Low
1|Medium
2|High
3|Urgent
apaderno
  • 28,547
  • 16
  • 75
  • 90
Chris Muench
  • 17,444
  • 70
  • 209
  • 362

2 Answers2

10

You can get it nice and quickly using field_info_field():

$key = 0; // Or whatever
$field = field_info_field('field_priority');
$label = $field['settings']['allowed_values'][$key];

There's a whole host of field functions in field.info.inc, they're very handy.

Clive
  • 36,918
  • 8
  • 87
  • 113
  • are form fields also fields so that you could use this function for a selected option in the validate/submit handler? – Alex Nov 11 '14 at 11:19
0

I would suggest to use list_allowed_values() instead of searching the array yourself:

$field = field_info_field('field_priority');
$options = list_allowed_values($field);
$label = $values[$key];

Or in short:

$label = list_allowed_values(field_info_field('field_priority'))[$key];
Ludo - Off the record
  • 5,153
  • 4
  • 31
  • 23