12

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?

20

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];
  • 5
    Geting the allowed values is basically the right way, but getting that can be easier: $state->getFieldDefinition('field_state')->getFieldStorageDefinition()->getSetting('allowed_values'). – Berdir Jan 4 '17 at 7:14
2

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();

// 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);
  • Welcome to Drupal Answers! Just curious, where is the label now OP is asking for? Inside $allowed_options? – leymannx Jun 27 '19 at 17:02
  • Thanks! Yes, it was inside $allowed_options. I edited my answer and added examples. – entity Jun 28 '19 at 19:30
0

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.

0

This might help as well:

$node->field_state->getFieldDefinition()->getSetting('allowed_values');

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.