How change user form mode programmatically? i create 2 custom user register form mode : 



When i go to mysite/user/register

Drupal show me the last form mode i create :


  S'inscrire


But when i go in the admin mode, for add a user, it show me the same form.
i try to display "Affichage Admin" form mode with more field.

So i try to alter the form mode programmatically.

Any idea how can i change form mode before it render ?
something like this:

/**
 * Implements hook_entity_type_alter().
 */
function YOUR_MODULE_entity_type_alter(array &amp;$entity_types) {
  // We get all form modes, for all entities, and loop through them.
  $form_modes = \Drupal::service('entity_display.repository')-&gt;getAllFormModes();
  foreach ($form_modes as $entity_type =&gt; $display_modes) {
    if ($entity_type !== 'user') {
      // We are only interested in adding a handler to the Node entity.
      continue;
    }

    $user_entity = $entity_types[$entity_type];
    foreach ($display_modes as $machine_name =&gt; $form_display) {
      if ($machine_name !== 'sinscrire') {
        // We are only interested in adding this form mode to the definition.
        continue;
      }
      // We get the default handler class. It will be enough for us.
      $default_handler_class = $user_entity-&gt;getHandlerClasses()['form']['default'];
      // We set the form class, keyed by the machine name of our custom form mode.
      $user_entity-&gt;setFormClass($machine_name, $default_handler_class);
    }
  }
}


Credit goes here: https://www.webomelette.com/render-custom-entity-form-modes-programatically-drupal-8