3

My users enter a phone number on registration. The amount of values that can be entered is set to unlimited. Setting this changes the field so that you can add another field below to add additional phone numbers. I would like the users to be able to add more numbers after registration without having to go to /user/%user/edit

I am trying to return just the phone number field on another page. However I cannot seem to get any of that form displayed on another page. When I have been displaying other forms I have called them like this:

drupal_render(drupal_get_form('user_profile_form'));

This does return part of the form, First Name, Last Name, Country (all custom fields) but not the phone number field. I also get quite a few errors
Undefined index: user_profile_form in drupal_retrieve_form()
Undefined index: #user_category in block_form_user_profile_form_alter
Undefined index: #user_category in overlay_form_user_profile_form_alter()
Undefined index: #user_category in system_form_user_profile_form_alter()
Undefined offset: 0 in uc_roles_form_user_profile_form_alter()

Obviously I am doing something very wrong...

  • 2
    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()." – Pawel Dubiel May 15 '12 at 10:43
5

I made this work based on the work by Erik. I guess you could do ['#access']=false for all the values you don't want.

//Provide a custom page at /custom-profile to access our custom profile form
function mymodule_menu() {
  $items['custom-profile'] = array(
   'title' => 'Custom Profile!',
   'page callback' => 'mymodule_custom_profile',
   'access callback' => TRUE, 
   'type' => MENU_NORMAL_ITEM,
 );
}

//displays the custom form at /custom-profile
function mymodule_custom_profile() {
  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;
}

function mymodule_form_user_profile_form_alter(&$form, &$form_state){
  //modifies the form_user_profile DO CUSTOM STUFF BELOW!
  if(isset($form_state['build_info']['args'][2]) && 
           $form_state['build_info']['args'][2] = 'custom'){

   //rename submit button
   $form['actions']['submit']['#value'] = t("dooo it!");

   //remove a few account things from this page
   $form['account']['#access']=FALSE;

   //needs to be there for the picture to work
   form_load_include($form_state, 'inc', 'user', 'user.pages');
  }
}
| improve this answer | |
3

Try this

module_load_include('inc', 'user', 'user.pages');
global $user;
print(drupal_get_form('user_profile_form', $user));

This will work.

| improve this answer | |
  • Excellent, yes this does display the user_profile_form. Do you know how to return a single field? In this case a field with the values limit set to unlimited. Also I have just noticed, the custom fields added to this form do not display their content. – dooffas Apr 3 '12 at 9:28
  • The AJAX on the 'unlimited' values field does not work also. Call to undefined function user_profile_form_validate() – dooffas Apr 3 '12 at 9:42
  • I think we cant return a single field only. Make a hook_form_alter and preload the values. Get values using user_load – niksmac Apr 3 '12 at 9:45
  • Try print drupal_render(drupal_get_form('user_profile_form')); – niksmac Apr 3 '12 at 9:49
  • I'm afraid drupal_render has no affect, I still have the Call to undefined function user_profile_form_validate() error. Do I have to load the validation for the form separately? I assumed that getting the form would automatically load the validation function. – dooffas Apr 3 '12 at 9:57
1
drupal_get_form('user_profile_form',$user,'account', 'my_parameter');      

function my_module_form_alter((&$form, &$form_state, $form_id) {
  switch($form_id) {   
      case 'user_profile_form':
        $info =  $form_state['build_info']['args'][2];
        if ( $info == 'my_parameter' ) {
           //do something with the form
           $form['my_field']['#access'] = false ;
           //prevent ajax errors
           form_load_include($form_state, 'inc', 'user', 'user.pages');
        } 
      break ;       
   }
}
| improve this answer | |
  • This doesn't work for me but does result it lots of php warnings... – Felix Eve Oct 15 '13 at 14:18
0

Based on @pawel-dubiel comment I added user.pages.inc directly to hook_menu():

function mymod_menu() {
   return array(
     'my-path' => array(
        'page callback' => 'mymod_page_callback',
        'file' => 'user.pages.inc',
        'file path' => drupal_get_path('module', 'user'),
     )
   );
}

Works like a charm and using hook_init() is avoided.

| improve this answer | |
-1

Trying to fetch a single field from a form doesn't make sense IMHO.

If you want to update a single value in the database for this field, you'd better code your own form and manage the submit and validate functions to update the database yourself.

| improve this answer | |

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.