How do I get the user Profile form? I tried the following:

$form = \Drupal::formBuilder()->getForm(ProfileForm::class);

but this results in :

Fatal error: Call to a member function getEntityTypeId() on null in /www/project/docroot/core/lib/Drupal/Core/Entity/EntityForm.php on line 77


Try this to load the user form programatically:

$user = \Drupal::currentUser();
$user_entity = \Drupal::entityTypeManager()

$formObject = \Drupal::entityTypeManager()
  ->getFormObject('user', 'default')

$form = \Drupal::formBuilder()->getForm($formObject);
|improve this answer|||||
  • I would refer this answer. This worked for me. – Pupil Mar 19 '19 at 9:36

You need to use it's namespace, like

use Drupal\user\ProfileForm;


$form = \Drupal::formBuilder()->getForm(ProfileForm::class);

Or prefix the class, like

$form = \Drupal::formBuilder()->getForm(Drupal\user\ProfileForm::class);

Or get it by 'name', like

 $form = \Drupal::formBuilder()->getForm('Drupal\user\ProfileForm');

To fix your error, you need to pass a user object there, like this (for the current user)

Drupal::formBuilder()->getForm(ProfileForm::class, Drupal::currentUser());
|improve this answer|||||
  • I am using it's namespace. I get the same results from all of those methods you posted. Would it make a difference that I'm trying to get it in a custom module controller? – Janey Jul 21 '16 at 22:01
  • ah - you probably need to pass the user ;) see my edit. – rémy Jul 21 '16 at 22:06

For entities like users use entityFormBuilder():

$form = $this->entityFormBuilder()->getForm($user);

entityFormBuilder() is available in all Controllers.

The first parameter is an entity, like node or user.

As second parameter you could choose a form. If empty 'default' is used and this is ProfileForm as defined in the user entity:

 *     "form" = {
 *       "default" = "Drupal\user\ProfileForm",
 *       "cancel" = "Drupal\user\Form\UserCancelForm",
 *       "register" = "Drupal\user\RegisterForm"
 *     },
|improve this answer|||||

This is how I did it in my custom controller:

class UserEditController extends ControllerBase {

   * Display the page content.
   * @return array
  public function content($user) {
    $entity = \Drupal::entityManager()

    $formObject = \Drupal::entityManager()
      ->getFormObject('user', 'default')

    $form = \Drupal::formBuilder()->getForm($formObject);

    return [
      '#theme' => 'template_user_edit_form',
      '#user_edit_form' => $form,

In the routing file I did:

   path: '/user/{user}/edit'
     _controller: '\Drupal\cusotm_module\Controller\UserEditController::content'
     _title: 'Update your profile'
     _permission: 'access content'

Big thanks to 4k4 who helped me getting the "default" to call the form !

|improve this answer|||||

Drupal 7

I suggest another approach that seems easier.

global $user;
$user = user_load($user->uid);
// Loading the module to have access to the form.
module_load_include('inc', 'user', 'user.pages');
// Get the form.
$user_form = drupal_get_form('user_profile_form', $user);
// Render the form.
return drupal_render($user_form);</code></pre>

With that you just have to know the $form_id by using hook_form_alter() and you can call all forms you want.

This solution is for Drupal 7.

|improve this answer|||||
  • The question is for Drupal 8. – kiamlaluno Jul 22 '16 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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