I want to programmatically get the list of possible key values for a field, along with their labels. For example:
1 => red 2 => blue 3 => Yellow ...
and so on. How do I do that?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.Sign up to join this community
It's just a matter of getting the field basic data, then get a list of possible values using Field API function that is part of List module.
$field = field_info_field([FIELD_MACHINE_NAME]); // This one gets the field data $field_values = list_allowed_values($field); // This one gets an array of values => labels $field_key = array_search('red', $show_field_values); // This one gets the key for the value labelled "red", that is: 1