Score:0

Render custom field from user registration form

zm flag

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.

4uk4 avatar
cn flag
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.
zm flag
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.
Score:2
cn flag

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');
    }
    
zm flag
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).
4uk4 avatar
cn flag
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.
zm flag
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.
zm flag
FYI....the contrib module has some errors and isn't working in Drupal 9 right now it looks like.
4uk4 avatar
cn flag
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.
zm flag
Oh I see. Actually....I did leave that detail out. This is an admin form. In other words, not accessible by anonymous users. The use case is that they want to let someone add users of a certain role, but don't want to give them access to the full registration form. I've that form completely built and works except I need one field from the main registration form that is an entity reference field. The path to the form is /admin/people/ch-create and everything is generated by the module I wrote. I'm just stuck at getting this one field. I haven't found much on programmatically reusing fields.
4uk4 avatar
cn flag
OK, then create an entity form route for the path. See the edited answer.
zm flag
Thanks for your patience with me. I didn't really achieve what I wanted to. I was never able to figure out how to load one single field from the main registration form into a new registration form on the admin side. Again there is very little documentation about programmatically reusing fields. But this did help me to completely recreate the main registration form and I just altered it to my needs and assigned roles to it upon completion. Thank you for your help!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.