How do I load a form with a specific form display (form mode)? On a custom page I'm trying to load an user edit form with a specific form display.
I'm able to load the form however I can't figure out how to load the form with the specific display.

This is what I have now:

public function content($substep) {

    $user = \Drupal::entityTypeManager()
      -&gt;getStorage('user')
      -&gt;load(1); // loading user 1 for test purpose

    $form_state = new FormState();

    $form = \Drupal::entityTypeManager()
      -&gt;getFormObject('user', 'default')
      -&gt;setEntity($user);

    return \Drupal::formBuilder()-&gt;getForm($form);
}


Anyone has an idea?
In D8 you can add display modes for forms in UI. But these form modes can't be used right away like view modes.

If you want to use a form mode in code you have to set a form operation with the same name in the handlers section of the entity type. The form class you use for this handler is the same as in the default handler, because you want to control which fields are displayed in the form mode, not in a different form class.

For example to use a form mode profile2, which you have configured in UI, you have to add a form operation profile2 to the user entity type definition in a hook_entity_type_build:

mymodule.module

function mymodule_entity_type_build(array &amp;$entity_types) {
  $entity_types['user']-&gt;setFormClass('profile2', 'Drupal\user\ProfileForm');
}


Then you can use the new form operation (form mode) in the code example from the question:

public function content($substep) {

  $user = \Drupal::entityTypeManager()
    -&gt;getStorage('user')
    -&gt;load(1); // loading user 1 for test purpose

  $form = \Drupal::entityTypeManager()
    -&gt;getFormObject('user', 'profile2')
    -&gt;setEntity($user);

  return \Drupal::formBuilder()-&gt;getForm($form);
}