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