0

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?

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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