I created several custom user profile fields, including Full Name (a plain text field) and County (a List (text) field). They are displayed on the /user/#/edit form. I want to display these fields (and several others) in a different form I am generating via custom module code.

Question: Is it possible to extract the definition of a field, including its label, default value, and, in the case of County, the list of allowed values?

I imagine a field is an entity of some kind that can be extracted, but after much reading, I haven't seen how. I'm likely just not using the right terminology.

I could easily hard-code replicas of these fields in the PHP+HTML form, but I want to maintain the label, default value, list of allowable values, etc. for them in just one place--as part of the user profile. Thus, I want to extract their definitions and output them into the form I build.

2 Answers 2


Yes you can get field definition programmatically with field name and entity name, by using EntityFieldManager::getFieldDefinitions. For your select list how to get allowed values check this example

 $options_array = [];
    $field_name = 'field_country_list';
    $definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('custom_entity_name', 'bundle');

    if (isset($definitions[$field_name])) {
      $options_array = $definitions[$field_name]->getSetting('allowed_values');

source : https://api.drupal.org/comment/63953#comment-63953

  • 1
    Thanks @berramou! This was the perfect answer. For anyone who may run across this, to get user profile fields you have added, the 'custom_entity_name' and the 'bundle' in the example above are both 'user'. If you use 'user' and null. for them, you won't get fields you've added. Apr 25, 2020 at 20:24
  • Welcome @Marshall glad that help!
    – berramou
    Apr 25, 2020 at 20:30

You can use a field widget from an entity form in a custom form as follows:

$entity_type = 'user';
$bundle = 'user';
// Alter accordingly if you are using custom form modes.
$form_mode = 'default';
$field_name = 'field_fullname';
$element_name => 'fullname';

if (!isset($form[$element_name]['#parents'])) {
  // Required widget to be embedded.
  $form[$element_name]['#parents'] = [];

$form_display = \Drupal::entityTypeManager()->getStorage('entity_form_display')->load($entity_type .'.' . $bundle . '.' . $form_mode);
if ($widget = $form_display->getRenderer($field_name)) {
  $items = $podcast->get($field_name)->filterEmptyItems();
  $form[$element_name] = $widget->form($items, $form, $form_state);
  $form[$element_name]['#access'] = $items->access('edit');

The resulting value is buried in the $form_state->getValues() array, and differs depending on the widget type, so you'll need to examine that array on submission to find the value you are looking for.

  • Thanks for this, @Jaypan. I decided to go with just getting the field values and styling my own fields, so berramou's answer worked for my purposes, but yours is also useful and may benefit someone looking for a similar solution. Apr 25, 2020 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.