I can't figure out to render the user profile form with a custom display mode. I did search the forum and internet and it seems that this piece of code should work:

 function mymodule_entity_type_build(array &$entity_types) {
      $entity_types['user']->setFormClass('my_custom_form_display_mode_id', 'Drupal\user\ProfileForm');

combined with

$user = \Drupal::entityTypeManager()
$formObject = \Drupal::entityTypeManager()
  ->getFormObject('user', 'my_custom_form_display_mode_id')
$form = \Drupal::formBuilder()->getForm($formObject);

However, the form that gets loaded actually contains all fields and is not even using the default form mode. What am i doing wrong?

  • The approach is correct, but you mix node and user. – 4k4 Jun 21 '18 at 18:30
  • Im sorry, i just copied the code from the other post on the forum, ill edit my post. However, with the new updated code it still not works. – Skywalker Jun 21 '18 at 18:51
  • now it's the same as drupal.stackexchange.com/questions/230527/…, can't tell what went wrong in your case, did you some basic debugging, for example checking if the code is run? – 4k4 Jun 21 '18 at 19:02
  • Not sure what to debug though, i mean the code gets executed but it just doesn't use any display mode. – Skywalker Jun 21 '18 at 19:06

Browse other questions tagged or ask your own question.