1

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!

2
  • Have you tried $request->get('type')->getString();? Commented Feb 4, 2022 at 13:13
  • that does not work it returns the same as ->value, thanks for the help :) Commented Feb 4, 2022 at 13:17

2 Answers 2

4

I could make this work by changing

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

to

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

This is working for me (Drupal 9):

 $request->get('type')->view()[0]['#markup']; 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.