16

I have a select list of states:

AL|Alabama AK|Alaska AZ|Arizona AR|Arkansas 

In code to access the selected state value I can run code like this:

... $entity->get('field_state')->value; ... 

I get the values like AL or AK. How can I get the labels like Alabama or Alaska?

2
  • Possible duplicate: drupal.stackexchange.com/questions/201064/… Commented Jan 4, 2017 at 5:08
  • 1
    That only pertains to getting the field's label. I am looking for a selected value from a select list. Commented Jan 4, 2017 at 5:43

5 Answers 5

26

Not sure if there is a better way but I got the value by getting the allowed values first.

$allowed_values = $state->getFieldDefinition('field_state')->getFieldStorageDefinition()->getSetting('allowed_values'); $state_value = $state->get('field_state')->value; $info['state_name'] = $allowed_values[$state_value]; 
1
  • 6
    Geting the allowed values is basically the right way, but getting that can be easier: $state->getFieldDefinition('field_state')->getFieldStorageDefinition()->getSetting('allowed_values'). Commented Jan 4, 2017 at 7:14
7

The proper way is to use options_allowed_values() function. Because the field might not have "allowed_values" setting and options can be provided through the "allowed_values_function".

Live examples:

Case #1: You have an entity object and want to get a label of the value of the field with options.

$field_name = 'field_state'; /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_definition */ $field_definition = $entity->{$field_name}->getFieldDefinition()->getFieldStorageDefinition(); // This is a list (array) of options for the field definition: // [{option1_key} => {option1_value}, {option2_key} => {option2_value}, ...] $field_allowed_options = options_allowed_values($field_definition, $entity); // If the field is NOT multiple. /** @var string|\Drupal\Component\Render\MarkupInterface $field_value_label */ $field_value_label = $field_allowed_options[$entity->{$field_name}->value]; // If the field is multiple. $field_value = array_column($entity->{$field_name}->getValue(), 'value', 'value'); /** @var string[]|\Drupal\Component\Render\MarkupInterface[] $field_value_labels */ $field_value_labels = array_intersect_key($field_allowed_options, $field_value); 

Case #2: You don't have an entity object but you want to get a list of all allowed options of the field of the entity.

$entity_type_id = 'node'; $field_name = 'field_state'; $entity_fields_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($entity_type_id); /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_definition */ $field_definition = $entity_fields_definitions[$field_name]; $field_allowed_options = options_allowed_values($field_definition); 
0
4

This might help as well:

$node->field_state->getFieldDefinition()->getSetting('allowed_values'); 
1
  • an even shorter way which works since Drupal 9 is $node->field_state->getSetting('allowed_values'); Commented Dec 10, 2024 at 7:24
1

On Drupal 8 you can use the view() method which will render the field, so you can use its markup:

$rendered_state_field = $entity->get('field_state')->view(['label' => 'hidden']); 

then, the path to the label of the selected value should be:

$rendered_state_field[0]['#markup'] 

If you want to use it in a twig, then you can simply pass $rendered_state_field as a variable.

-1

drupal 7 way:

$jid = $row->job_id_181; if($jid) { $jobNode = node_load($params['jid']); $key = $jobNode->field_job_location['und'][0]['value'];//2 if($key) { $field = field_info_field('field_job_location'); $label = $field['settings']['allowed_values'][$key]; echo $label; } } 

Hope it helps some one in future.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.