Score:1

Get value from list_string instead of key

ph flag

I have this field created through baseFieldDefinition

$fields['type'] = BaseFieldDefinition::create('list_string')
        ->setRevisionable(TRUE)
        ->setLabel(t('Type'))
        ->setRequired(TRUE)
        ->setSettings([
            'allowed_values' => [
                'house'    => 'House',
                'business' => 'Company',
            ],
        ])
        ->setDisplayOptions('view', [
            'label' => 'visible',
            'type' => 'list_default',
            'weight' => 6,
        ])
        ->setDisplayOptions('form', [
            'type' => 'options_select',
            'weight' => 6,
        ])
        ->setDisplayConfigurable('view', TRUE)
        ->setDisplayConfigurable('form', TRUE);

But when i get the entity in php rest resource

$request = RequestEntity::load($id);

$request->get('type)->value
// Returns key 'business' instead of value 'Company'

Is there a way to fix this?

Any help is welcome!

Lambic avatar
ph flag
Have you tried `$request->get('type')->getString();`?
simp you avatar
ph flag
that does not work it returns the same as ->value, thanks for the help :)
Score:3
ph flag

I could make this work by changing

$request->get('type)->value

to

$request->house_type->getSetting('allowed_values')[$request->get('house_type')->value]
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.