0

In Drupal 7, I have a page content type that has a field called group (so the machine name is field_group). The field is a List (integer) field type.

This field might have allowed values as follows:

0|Description 1
1|Description 2
2|Description 3

If I want to see the value of this field for the current page, the following code correctly works:

echo render(field_view_field('node', $node, 'field_group'));

If the key were set to 1 for this page, the above code would display the value "Description 2."

However, is there a way to instead get the key (e.g., 0, 1, 2, etc.)?

1

You can change the display in your content type "Manage Display" and ask to have the key and not the label :) It's here : /admin/structure/types/manage/[your type]/display

Programmatically for a nude value as asked :

$items = field_get_items('node', $node, 'field_list'); 
$key = $items[0]['value'];
  • Comments are not for extended discussion; this conversation has been moved to chat. – Clive Nov 7 '17 at 19:08

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.