For example, There is a field with 3 options in allowed values, like option1, option2, option3.Is there any function which receive the field name and returns selected values not a keys ?

With this just return key of selected opinion

  print ($node->field_opinion['und'][0]['value'])

Update :

I have create select list field and with this field user can answer and select own value like this


With above code just return the keys field and i want to return values like "Stars"

  • Can you please provide us more information? – Jeetendra Kumar Feb 21 '18 at 6:10
  • @Jeetendra Kumar UPDATE my friend ! – Bijan Zand Feb 21 '18 at 6:25

Try this code:

$key = $node->field_opinion['und'][0]['value'];
$label = list_allowed_values(field_info_field('field_opinion'))[$key];

  • Can i use that for checkbox forms ? – Bijan Zand Feb 21 '18 at 11:44

You need the field_info_field() and list_allowed_values() functions to get an array representation of the values you entered in the field's configuration.

The following:

$field_opinion_info = field_info_field('field_opinion');
$opinion_values= list_allowed_values($field_opinion_info);

should give you access to an array that looks like this stored in $opinion_values:

    [1] => Stars
    [2] => Nights
    [3] => Awesome
    [4] => Days

And from that, you can get the needed value like this:

$selected_option = $node->field_opinion[LANGUAGE_NONE][0]['value'];
$selected_value = isset($opinion_values[$selected_option]) ? 
  field_filter_xss($opinion_values[$selected_option]) :
 t('Not a valid option');

Note the use of field_filter_xss(); according to the documentation of list_allowed_values():

The strings are not safe for output. Keys and values of the array should be sanitized through field_filter_xss() before being displayed.

Hope this helps!

  • Thanks @Stefanos Petrakis but result Must have selected value, your code return all value ! – Bijan Zand Feb 21 '18 at 6:40
  • @BijanZand That is also described in the answer, look for the $selected_value variable in my code example. And pay attention to the use of field_filter_xss function. – Stefanos Petrakis Feb 21 '18 at 6:42

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.