i create 2 custom user register form mode :

enter image description here

When i go to mysite/user/register

Drupal show me the last form mode i create :


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 ?

  • You display a screenshot of UI, seems like the first duplicate is right, but I've added a second one for programmatically
    – 4uk4
    May 26, 2017 at 8:32
  • You right, i see your post, but it's for custom form, not for alter admin form
    – Kevin
    May 26, 2017 at 8:35
  • Don't understand why custom form or admin form is relevant here, you can use form modes for all entity forms. The question is UI only. If it is programmatically, can you show some code?
    – 4uk4
    May 26, 2017 at 8:41
  • Oki, i ll reform this question
    – Kevin
    May 26, 2017 at 8:43
  • This post not duplicate. Answer for this is here: drupal.org/forum/support/module-development-and-code-questions/…
    – emamie
    Feb 17, 2019 at 4:24

1 Answer 1


something like this:

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

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

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

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