[Drupal Field API] How to get the list of allowed values of a field in Drupal 7

| | 2 min read

Many Drupal users wanted to know how to get the list of allowed values in a field in Drupal 7. If you have faced with the same question just like me while using Drupal field module in Drupal 7 core then read on to find out the solution.

In Drupal 7.x the field module in core helps Drupal developers and administrators to create and manage different types of content. It is also possible to set a list of values as allowed values in the field configuration. When we develop modules to scale Drupal we may need to know the list of allowed values of a field. Have a look at the solution we have come up with.

Solution

To get the list of allowed values of a field you have to first get the information about the field. Here is the quick and dirty way to get the field information.

$all_fields_info = field_info_fields();

The function field_info_fields() returns an array of the field information of all the fields in your Drupal site. Now you have to use this array to get the allowed values of the particular field.

$allowed_values= list_allowed_values($all_fields_info["your_field"]);

list_allowed_values() is the function which returns the allowed values list of the field as an array. Now you can use the array $allowed_values according to your needs. You can use this as the options of a select box of your custom form or anywhere related to the field.

Hope this info was helpful. Share it with others and use the comment form if you have any queries.