0

I'm trying to render a custom field that shows up on the user registration form on a separate new form that creates users of a particular role with specific permissions.

I have everything working except being able to render this custom field.

When I view the field under the user registration form settings page, it says it's an entity reference field. When it renders it has 4 check boxes.

I've figured out how to kint out the fields that are on the user registration form from my new form, but I can't figure out how to render the specific one that I need.

$entityTypeManager = \Drupal::service('entity_field.manager');
$fields = $entityTypeManager->getFieldDefinitions('user', 'user');

foreach ($fields as $field_name) {
  kint($field_name);
}

That gives me a listing of all of the fields in the user registration.

I feel like I'm missing something very obvious.

2
  • Normally you would create a custom form mode and configure the field widgets you need. I think you are asking the question because you built the form from scratch. This is easy for simple field types, but a lot of work for complex ones.
    – 4uk4
    Sep 17, 2021 at 13:24
  • Yeah it's a from scratch build within a module. This has to be rolled out and turned on in production, which we have minimal control over because of a federal client. I'm finding very little on creating custom registration forms and reusing fields from user forms. Sep 17, 2021 at 13:43

1 Answer 1

2

A form mode is configuration which you can roll out via CMI (drush cex/cim) or install with the module by including the YAML files in the modules config folder.

Implementing a new register form mode is not very intuitive. The basics:

  1. Create the form mode:

    /admin/structure/display-modes/form
    
  2. Enable and configure it like in this questions for the pre-installed Register form mode:

    How to hide custom fields on the registration form?

  3. To place the new form mode on a page you need either a contrib module

    https://www.drupal.org/project/form_mode_manager

    or custom code:

    How do I load a form with a specific form display (form mode)?

    or a custom route:

    mymodule.routing.yml

    mymodule.register2:
      path: '/admin/people/ch-create'
      defaults:
        _entity_form: 'user.register2'
        _title: 'Create new account'
      requirements:
        _permission: 'register certain users'
    

    register2 needs to be registered as form handler in the entity type:

    mymodule.module

    function mymodule_entity_type_build(array &$entity_types) {
      $entity_types['user']->setFormClass('register2', 'Drupal\user\RegisterForm');
    }
    
8
  • Ok so if I understand you correctly, the user registration form has a form mode that is not accessible without jumping through some hoops (ie using this contrib module). Is that correct? I'd much rather code this instead of leaning on a contrib module, but let me look into this a little bit first. Contrib modules have to be approved and signed off on (just the natural of some fed clients). Sep 17, 2021 at 14:16
  • The pre-installed Register form mode is the only form mode which works out of the box without additional code. For a custom form mode, like Register 2, you need either a contrib module or custom code.
    – 4uk4
    Sep 17, 2021 at 15:00
  • So I'm still circling around this. I'm not really trying to create a new form mode. I'm fine with using the out of the box register form mode. I'm just trying to get an entity reference field that was added on to the user registration form into a second user registration form that creates a user with a unique role. I can access all kinds of information about the field via kint, but I can't get it to render from the buildForm function. Everything else renders fine. I guess I don't understand how a form mode is needed here if I can access the field data. Sep 19, 2021 at 21:46
  • FYI....the contrib module has some errors and isn't working in Drupal 9 right now it looks like. Sep 20, 2021 at 1:14
  • These two solutions are only examples to make clear what needs to be done, there are a wide range of other solutions for step 3 like building a custom form route. If you are stuck at this point ask a question with details where you want to place the second form on your site.
    – 4uk4
    Sep 20, 2021 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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