2

I created new menu item witch I want to insert a custom form to edit some user fields :

$items['user/%user/edit-custom'] = array(
  'title' => 'Knowladge',
  'page callback' => 'drupal_get_form',
  'page arguments' => array('user_profile_form_custom', 1),
  'access callback' => 'user_edit_access',
  'access arguments' => array(1),
  'type' => MENU_LOCAL_TASK,
);
return $items;

Then the custom function that return the form :

function user_profile_form_custom(){
  module_load_include('inc', 'user', 'user.pages');
  
  global $user;  
  $user = user_load($user->uid);

  $form = drupal_get_form('user_profile_form', $user, 'account', 'custom'); 

  return $form;
}

When visiting "user/%user/edit-custom" page I can see the profile page, then with some logic code in hook_form_alter I can hide some fields and show up only needed fields...

But the problem that I can't save the form, I get this error :

Fatal error: Call to undefined function user_profile_form_validate() in path_to_drupal/includes/form.inc on line 1514

this solution is inspired from answers described in this post .

Have you any idea please how to solve this, or if you have another way to get some user fields, edit them in a custom page, and save the changes.

Thanks in advance.

3 Answers 正确答案

0

Is it possible you missed the comment (below)? It's on the post you linked to.

1)  "module_load_include('inc', 'user', 'user.pages'); should be added to hook_init()otherwise all profile form functionalities that require AJAX calls will return the error 'Call to undefined function user_profile_form_validate().'"

2) 在form_alter中,给这个表单加上一句  form_load_include($form_state, 'inc', 'user', 'user.pages');




  • yes, I tried also to add "module_load_include" in hook_init but I get another error : Fatal error: __clone method called on non-object in path_to/modules/user/user.pages.inc on line 313  Jun 3, 2014 at 21:52
  • @Boussettamoez - It's telling you that $user is not an object. Did you keep the rest of your code in user_profile_form_custom()? I'm not sure if it's related, but you shouldn't have to call user_load() after global $user;. But I'm not even sure the global user is what you want. Don't you want the user whose ID is in the menu path (%user)?  Jun 3, 2014 at 22:33
  • @Boussettamoez - In user_profile_form_custom(), unless I'm misunderstanding what you are trying to do, I would suggest getting your user this way: $user = user_load(arg(1));.  Jun 3, 2014 at 22:41
  • Sorry, but I get the same error even if I use user_load(arg(1));!  Jun 4, 2014 at 7:34
  • But you will get the right user. :-) You're right about $form and &$form_state (below). Amazed that I overlooked that. I believe it's required.  Jun 4, 2014 at 15:11
0

Adding the parameters $form and &$form_state in the custom function must correct the bug!

0

Instead of module_load_include(), use form_load_include().

Here's the code:

global $user;
$account = user_load($user->uid);

$form_state = array();
$form_state['build_info']['args'] = array($account);

form_load_include($form_state, 'inc', 'user', 'user.pages');

$form = drupal_build_form('user_profile_form', $form_state);

$output = drupal_render($form);

return $output;

Your Answer


来自  https://drupal.stackexchange.com/questions/116156/create-a-custom-form-based-on-user-fields