2

I have a Views page and I want to insert the user creation form as found in /admin/people/create into the Views page header. I know people frown upon the PHP filter, but I just need to output the user registration form, which is at most 2 lines of php code vs having to make a module just for that. Unless there is a better way?

I know drupal_get_form() has been replaced from the form builder in Drupal 8, and the Drupal 8 equivalent of drupal_get_form() is, for example, $form = \Drupal::formBuilder()->getForm(Drupal\user\Form\UserLoginForm::class);.

But how do I get the form name or ID? How do I put it in that example? Do I just inspect the form and grab the form ID?

enter image description here

I tried the following code, but I nothing gets returned. What could I be missing?

$form = \Drupal::formBuilder()->getForm(Drupal\user\Form\user_register_form::class);
drupal_render($form);

5 Answers 5

3

Its a bit more complicated than render a login form... Here is the correct way to do this:

$entity = \Drupal::entityTypeManager()->getStorage('user')->create(array());
$formObject = \Drupal::entityTypeManager()
  ->getFormObject('user', 'register')
  ->setEntity($entity);
$form = \Drupal::formBuilder()->getForm($formObject);
$form_rendered = \Drupal::service('renderer')->render($form);

PS. Found this solition here: http://web-tricks.org/content/how-render-user-login-form-and-user-register-form-drupal-8

4
  • how can you see all the form objects? say i want to render a specific nodes edit form... Commented Jun 29, 2017 at 11:37
  • @duckx I think there's no any universal way to do this... Commented Jun 29, 2017 at 15:29
  • @duckx to render a specifi node form - use this code $form = \Drupal::service('entity.form_builder')->getForm($entity); Commented Jun 29, 2017 at 15:30
  • How do you find out it's entity.form_builder ? Is there a list somewhere where I can see all the possible inputs? Commented Jun 29, 2017 at 16:23
3

You could use the Form Block module and place it before/above your view.

Enables the presentation of user registration, site wide contact, or node creation forms in blocks. This is particularly useful for including forms on panels.

Do I just inspect element the form and grab the form id again and replace it with the UserLoginForm section?

You just need the ID user-register-form which means your form ID is user_register_form

I see now that you're talking about the admin form, I believe you just have to modify the formblock.module file and comment out or remove this part

 // Don't display the form to logged in users or if registration is disabled
  if (!$user->uid && variable_get('user_register', 1)) {
    $block['content']['form'] = drupal_get_form('user_register_form');
    $block['subject'] = t('Create new account');
    return $block;
  }

So try:

$parameter = 'new';
$form = \Drupal::formBuilder()->getForm('user_register_form', $parameter);
10
  • how do you render? $form = \Drupal::formBuilder()->getForm(Drupal\user\Form\user_register_form::class); then a drupal_render($form) --- it returns blank. Commented Jul 28, 2016 at 3:46
  • @duckx try $form = \Drupal::formBuilder()->getForm(Drupal\user\Form\UserRegisterForm::class);
    – No Sssweat
    Commented Jul 28, 2016 at 3:57
  • nope that didnt work: prntscr.com/byghwo Commented Jul 28, 2016 at 4:01
  • And your module doesnt seem like it works neither as I think its not compatible with drupal 8.1 ... I don't see any user registration form block in the blocks page. Commented Jul 28, 2016 at 4:02
  • @duckx you need to click on the [place block] button and then search for it.
    – No Sssweat
    Commented Jul 28, 2016 at 4:03
2

I still would like to know how to do it in PHP, but no one has a clear answer on how to render a drupal form in D8.

Instead of code, I just used this module that exposes form blocks in the blocks page: https://www.drupal.org/project/entityform_block

Looks like this:

enter image description here

1

You are referring the wrong form class.

  • UserLoginForm is the class handling the login form
  • user_register_form is not an existing class, in Drupal 8

What you are looking for is the class for the registration form (i.e. \Drupal\user\RegisterForm). The annotation for the User entity class tells you which class handles that form.

"form" = {
  "default" = "Drupal\user\ProfileForm",
  "cancel" = "Drupal\user\Form\UserCancelForm",
  "register" = "Drupal\user\RegisterForm"
}

Notice that instead of using \Drupal\user\RegisterForm::class, you can simply use '\Drupal\user\RegisterForm', since the method for building a form accepts also a string.

1
  • 1
    But how do you find the names of these classes? Back in Drupal 7, I can just do an inspect element in chrome and just view the Form ID in the html and just use that. What is the equivalent to this in Drupal 8? Thanks. Commented Jul 28, 2016 at 14:08
1

I had the same issue with this. There are two ways you can do this, but the first way doesn't send any notification emails; it simply registers the user. The second way registers users as normal. Choose the one you need based on your situation. Note that there's different syntax in both examples for doing the same things. Ideally you'd use dependency injection too, rather than loading services directly from the container.

First method (no emails, just saves the entity):

$user = User::create();
$form = \Drupal::entityTypeManager()
->getFormObject('user', 'default')
->setEntity($user);
return \Drupal::formBuilder()->getForm($form);

Second method (full registration):

$entity = \Drupal::entityTypeManager()
->getStorage('user')
->create(array());
$formObject = \Drupal::entityTypeManager()
  ->getFormObject('user', 'register')
  ->setEntity($entity);
return \Drupal::formBuilder()->getForm($formObject);
2
  • Where do you put this code in the theme file? Commented Mar 1, 2018 at 17:09
  • You don’t put it in the theme file. Put it in a custom module. Where are you trying to render it? Commented Mar 1, 2018 at 17:10

Your Answer

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

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