Monday 18 March 2019

Drupal 8: Get User Profile Field Allowed values list


Say, you have a user profile field: field_phone_brand

And you have options:

iPhone|iPhone
Samsung|Samsung
MI|MI
Oppo|Oppo
Vivo|Vivo

And you want to get the allowed values list in a custom module/theme,

$entityManager = \Drupal::service('entity_field.manager');
$fields = $entityManager->getFieldStorageDefinitions('user', 'profile');
$options options_allowed_values($fields[$fieldName]);

If you print:

echo '<pre>';
print_r($options);
echo '</pre>';

You will get:

Array
(
    [iPhone] => iPhone
    [Samsung] => Samsung
    [MI] => MI
    [Oppo] => Oppo
    [Vivo] => Vivo
)

No comments:

Post a Comment

Parenting tips to inculcate learning habits in your kid

Parenting tips to inculcate learning habits in your kid Tip #1) Children do not learn things, they emitate. So, try to do things by yours...