3

I have a module that i want to use to display a modified version of one of Drupal 7's default forms. The form ID is user_profile_form and it is the form used to edit a user's account information.

So this is the structure of my module:

  1. hook_menu with a page callback to a function --> user_exe_edit() -->
  2. function user_exe_edit() calls drupal_get_form(input_simple_form) -->
  3. function input_simple_form($form, &$form_submit)

Now I need the function input_simple_form to return the Drupal form with id "'user_profile_form".

How do I do that?

8

user_profile_form() is an existing function, but it's not defined in a module file; that means that the file where the function is defined needs to first be loaded, in a way or the other.

Instead of defining a page callback that then calls drupal_get_form() to get a form defined in a form builder implemented in the same module, I would rather define the menu callback using the following code (the code I am writing is for Drupal 7, but can easily adopted for Drupal 6, if there is the need.

$items['path'] = array(
  'page callback' => 'drupal_get_form',
  'page arguments' => array('input_simple_form'),
  'file path' => drupal_get_path('module', 'user'),
  'file' => 'user.pages.inc',
);

The code for the form builder could be something similar to the following one:

function input_simple_form($form, &$form_state) {
  $form = array_merge($form, drupal_get_form('user_profile_form', $account));
  // ...
}
  • user_profile_form() uses a third argument, the category of the profile to show. Its default value is "account"; if you are getting an error about a missing third argument, then you can explicitly pass that parameter too, for example using its default value, as in drupal_get_form('user_profile_form', $account, 'account').
  • "user.pages.inc" is the file containing the user_profile_form() form builder, in Drupal 7, and Drupal 6. The reason that you get the error you see is that such files are loaded only when Drupal is instructed to load them; that is done by using the "file" and "file path" array indexes. ("file path" is only required when the file is not between the files of the module implementing hook_menu().)
  • I used input_simple_form() as form builder, in the case you need to use its own validation and submission handlers.
  • There is a $account variable that has not been defined in the example code I wrote. That is one of the required arguments that user_profile_form() needs, as the function needs to know for which user account the form is being build. That is the other reason to use your own form builder, which should load the user object for the account used for the user profile form, or pass the value of the global $user to drupal_get_form('user_profile_form').
  • excellent answer kiamlaluno. I am using the array_merge function as you indicated and passing it $account which i'm getting from global $user; $account=$user->uid; However I'm getting the error Missing argument 3 for user_profile_form(). when you say account, do you mean the user id? – Carlos Muñiz Jun 6 '11 at 15:11
  • @Charlie No; it's a user object, the same object returned from user_load(), or contained in the global variable $user. I am going to update the answer to reply about the third argument. – kiamlaluno Jun 6 '11 at 15:23
  • 1
    Also note that you probably will have to set validate/submit callbacks explictly on the form, because the default name based detection will not work as you are calling it through a different form id. – Berdir Jun 6 '11 at 17:16
  • thank you for the very helpful answers. for other users who may have the same issue: the form 'user_profile_form' (and possibly other Drupal default forms) are not registered functions by default. You will have to load the file in which the function resides. In this case it is in 'user.pages.inc' in the 'user' module directory. after loading the file, make a proper call to drupal_get_form() as indicated by kiamlaluno above... – Carlos Muñiz Jun 7 '11 at 2:08
  • yes Berdir, I am validating the callbacks because otherwise people could enter certain parameters in the url to get the form rendered, specially because I'm using the user id as a callback parameter... thanks. – Carlos Muñiz Jun 7 '11 at 2:10
1

I'm not sure if I'm misunderstanding the problem, but is

return drupal_get_form('user_profile_form');

What you are looking for?

In the case of that particular form, it is defined in an inc file which is probably automatically included by the menu system. You'll need to run

module_load_include('inc', 'user', 'user.pages.inc') 

before you call drupal_get_form to ensure that the form building function is available to Drupal before you call it.

  • correct me if i'm wrong, but according to the FAPI drupal_get_form() needs to call a valid function. that's why i'm calling input_simple_form. If I call drupal_get_form('user_profile_form'), i get an error. Or am I doing this the wrong way?? – Carlos Muñiz Jun 6 '11 at 13:48
  • when I call drupal_get_form('user_login_block') the form is displayed correctly. so apparently user_profile_form is a function but drupal can't find it. I looked for the function and it's inside root/modules/user/user.pages.inc at line 242 in Drupal 7. how can my module have access to this form function? – Carlos Muñiz Jun 6 '11 at 13:59
  • The reason that you're getting an error is that user_profile_form is defined in an include (core/modules/user/user.pages.inc, line 242). Try module_load_include('inc', 'user', 'user.pages.inc') before calling drupal_get_form(). for reference: api.lullabot.com/user_profile_form/7 – cam8001 Jun 6 '11 at 14:41
  • 1
    There is no need to call module_load_include(); using the correct menu callback definition is possible to let Drupal load the include file that is part of another module. – kiamlaluno Jun 6 '11 at 17:25

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.