1

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 :

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 ?

marked as duplicate by 4k4 8 May 26 '17 at 8:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • You display a screenshot of UI, seems like the first duplicate is right, but I've added a second one for programmatically – 4k4 May 26 '17 at 8:32
  • You right, i see your post, but it's for custom form, not for alter admin form – Kevin May 26 '17 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? – 4k4 May 26 '17 at 8:41
  • Oki, i ll reform this question – Kevin May 26 '17 at 8:43
  • This post not duplicate. Answer for this is here: drupal.org/forum/support/module-development-and-code-questions/… – emamie Feb 17 at 4:24
0

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.
      continue;
    }

    $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.
        continue;
      }
      // 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.