I've been struggling these past two days trying to print the user profile form on a custom node, however I cannot seem to get it right.

I have tried printing drupal_edit_form:

// THIS kinda works, but no submit button
global $user;
$uid = $user->uid;
foreach ($user as $key => $value) {
  $account[$key] = $value;
}
print(drupal_get_form('user_edit_form', $uid, $account));

Invoking the user module:

// no success either
global $user;
print module_invoke('user', 'form', $user, 'account'));

Trying to print user_profile_form also returns an empty form... I'm kind of puzzled by this problem, has any one ever gotten this to work?

Comments

mattyoung’s picture

How about this:

global $user;
print user_view($user);
manuel garcia’s picture

Thanks for the input mattyoung, didn't think of that :)
However, that gets me fatal error call to undefined function for some bizarre reason.

I checked the user_view api page, and did what that function does in my code directly, like:

global $user;
user_build_content($user);
print(theme('user_profile', $user));

Which works, but just gets me the user profile page, no user edit form. Interesting, perhaps useful to some, but not in our case unfortunately...
I have investigated a bit more, and according to the api, user_edit() returns the user_profile_form, but I just cant seem to use it properly. If i do:

global $user;
print(drupal_get_form('user_profile_form', $user));

I get an empty form like this:

<form id="user-profile-form" method="post" accept-charset="UTF-8" action="/custompagetest">
<div>
  <input type="hidden" value="form-66e7e88618a9a5648d5519b2cefe0d84" id="form-66e7e88618a9a5648d5519b2cefe0d84" name="form_build_id"/>
  <input type="hidden" value="4b2158f030338ebde3462ce04d8fea6e" id="edit-user-profile-form-form-token" name="form_token"/>
  <input type="hidden" value="user_profile_form" id="edit-user-profile-form" name="form_id"/>
</div>
</form>

So it seems that's the right form to call, only it's not geting populated with anything. I have a feeling I am missing a step before i call drupal_get_form here. Anyone has a clue what could it be?

mattyoung’s picture

The function is in user.pages.inc which is not automatically loaded by the module system. So do this:

include_once drupal_get_path('module', 'user') . '/user.pages.inc';
global $user;
print user_view($user);
arcaneadam’s picture

Are you getting "Save" and "Delete" buttons when you use drupal_get_form('user_profile_form', $account, $category);
and have you simply tried:

global $user;
print user_edit($user, 'account');

Adam A. Gregory
_____________________________
Drupal Developer and Consultant
https://goo.gl/QSJjb2
Acquia Certified Developer-Back end Specialist

manuel garcia’s picture

If i do:

global $user;
print(drupal_get_form('user_profile_form', $user));

I just get an empty form like this (in the source):

<form id="user-profile-form" method="post" accept-charset="UTF-8" action="/custompagetest"> 
  <div>
    <input type="hidden" value="form-b346c2189fb4836866b69efea2874f67" id="form-b346c2189fb4836866b69efea2874f67" name="form_build_id"/>
    <input type="hidden" value="fba4df5987c9ce74afe016edfd13f4b2" id="edit-user-profile-form-form-token" name="form_token"/>
    <input type="hidden" value="user_profile_form" id="edit-user-profile-form" name="form_id"/>
 </div>
</form>

So the only way seems to be including user.pages.inc and then call user_edit directly. Why I don't know, since user_edit() basically does the same thing. Something for an X-files episode?

manuel garcia’s picture

OK guys, here is how to go about doing this:

global $user;
// if the user is logged in
if ($user->uid > 0) {
  include_once drupal_get_path('module', 'user') . '/user.pages.inc';
  print user_edit($user);
}
else {
// do something else
}

I've included a check for a logged in user, because otherwise it will still show the form, with no values in it, which is definitely not what you'd want.

Anyway, thanks SO much for your help, I knew there must be a way... <3 Drupal!

manuel garcia’s picture

OK, looks like drupal_get_form('user_profile_form', $user) will only work properly if you have previously included user.pages.inc.

So, if you don't want the page title to be set to the user name (done in user_edit), you would go about doing it like this:

global $user;
$uid = $user->uid;
if ($uid > 0) {
  include_once drupal_get_path('module', 'user') . '/user.pages.inc';
  print(drupal_get_form('user_profile_form', $user));
}
else {
// do something else
}

And that gets you just the form itself, populated and with all the buttons.

Again, thanks all for your help!

Bilmar’s picture

I came across this thread and tried the above and it worked perfectly! Thank you-

I have an additional question if someone can help:
Is it possible to just print one of the fields from the user profile form? for example the 'Email' input field?

I would appreciate your help!

andrefy’s picture

There is a better way to include a drupal module

<?php
global $user;
// if the user is logged in
if ($user->uid > 0) {
  module_load_include('inc', 'user', 'user.pages');
  print user_edit($user);
}
else {
// do something else
}
?>

Look at
http://api.drupal.org/api/function/module_load_include

Kind Regards
Andres Yajamin

manuel garcia’s picture

Very true, thanks!

ericdfields’s picture

I had to change one thing about this:

print user_edit($user);

to

return user_edit($user);

With print, we bypass the theme rendering. Return builds the form in the theme as expected.

jduhls’s picture

This worked just like I needed!

kcorbin’s picture

If you try to use the above code in a customized version of the user_edit_profile template it will not work as it sends you into an endless loop.

ANy ideas how to do it in that case?

Vahalaman’s picture

This snippet works great for the profile form but can this be applied to a cck form as well? Do you know what I would have to change in the 'drupal_get_form'(if that's even correct) to get it to print a cck form anywhere on a site?

bozho’s picture

Hi Manuel

How did you managed to redirect user edit form to a custom node?
I'm using a my own theme and when I try to edit the users profiles I receive a empty form, like in the one of the comments.
I wasn't be able to fin out the reason, but probably using a custom node will be a good workaround.

crossmedia’s picture

If you configured extra profile fields like, first name, last name, address, etc... under category "Registration Information" while using core profile module then it would be very easy for you to print edit profile form (Registration Information ) on any node, just use the below code in drupal 6.

global $user;
$uid = $user->uid;
if ($uid > 0) {  
include_once drupal_get_path('module', 'user') . '/user.pages.inc';  
$profile =  profile_load_profile($user);
print(drupal_get_form('user_profile_form', $user, 'Registration Information'));
}
fildawg’s picture

When I use this code, the labels of my form are displayed. However, the page doesn't show the input fields and the source doesn't have form tags. Any ideas where to debug this?

rmathew’s picture

In case anyone trying to do this in D7 stumbles across this page, here's how to render the user profile form in a PHP block:

<?php
module_load_include('inc', 'user', 'user.pages');
global $user;
print(drupal_render(drupal_get_form('user_profile_form', $user)));
?>
joekers’s picture

I've tried the code above and many other things but I just can't seem to populate the fields with the user's information. The username and email populate but all the other fields don't. If I got to 'user/uid/edit' the fields are there with data in them. Any ideas?

Edit: I was being stupid, for anyone else like me remember to user_load after global $user ;)

MKorostoff’s picture

The example by rmathew will only work if you have no custom fields. If you have custom fields then it's:

module_load_include('inc', 'user', 'user.pages');
global $user;
print(drupal_render(drupal_get_form('user_profile_form', user_load($user->uid))));
jaypan’s picture

Note that that will throw a PHP warning. To get rid of the warning, you should do this:

module_load_include('inc', 'user', 'user.pages');
global $user;
$form = drupal_get_form('user_profile_form', user_load($user->uid));
print(drupal_render($form));

Contact me to contract me for D7 -> D10/11 migrations.

DedSec’s picture

Thanks for this!