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

5 Answers 5

25

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').
    – Berdir
    Commented Jan 4, 2017 at 7:14
6

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
3

This might help as well:

$node->field_state->getFieldDefinition()->getSetting('allowed_values');
0
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.