In Drupal 7, how do you list all the allowed values in a certain field of some content type?
You can use the list_allowed_values function.
Lets see it with an example.
I will define a new Content Type called “Actividad
” (machine name=’actividad
‘), which will have a title, a body and a List(text)
field type called ‘tipo_actividad
‘ (machine name=’field_tipo_actividad
‘):
And then I will define the allowed values for this field_tipo_actividad
field:
Now, how do I programmatically list the allowed values for that field? Using list_allowed_values function:
$field = field_info_field('field_tipo_actividad'); /* field_tipo_actividad=machine name of the field */ $allowed_values = list_allowed_values($field); /* what are the values allowed for that field? */ return var_dump($allowed_values); |
If you want a prettier output, you can change the code to:
$field = field_info_field('field_tipo_actividad'); $allowed_values = list_allowed_values($field); $salida = ""; foreach ($allowed_values as $key=>$value){ $salida = $salida."Key: ".$key." Value: ".$value."<br />"; } return $salida; |
And you’ll get something like:
The post Drupal7: list allowed values from a field type list [SOLVED] appeared first on Lecciones Prácticas.