2

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

4

Try this to load the user form programatically:

$user = \Drupal::currentUser();
$user_entity = \Drupal::entityTypeManager()
  ->getStorage('user')
  ->load($user->id());

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

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

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());
  • 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
1

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"
 *     },
1

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()
      ->getStorage('user')
      ->create(array());

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

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

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

In the routing file I did:

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

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

1

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.

  • 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.