Skip to main content
added 924 characters in body; edited tags
Source Link
Antonio
  • 407
  • 6
  • 20

I have an idea but not sure will it work:Any suggestions? thx!

  1. Implement hook_field_attach_form to attach the field to event registration form and load value from Profile entity.
  2. Implement hook_field_attach_submit to update/save data to Profile entity.
 

I would use theEDIT: I implemented this functionality. For those interested, complete source code is below to update user field: (Note that I'm saving data to profile2!)

/**
*
* Implements hook_form_alter
*
*/
function loadMYMODULE_form_alter(&$form, user&$form_state, object$form_id) {
$existingUser    global $user;
    if ($form_id == 'registration_form') {
            $profile = user_loadprofile2_load_by_user('USERID'$user->uid);

  // createLoad anprofile2 arraydata
 of properties to update        if ($profile['main']->field_linkedin_url) {
$edit                $linkedin_url = $profile['main']->field_linkedin_url['und'][0]['value'];
            } 
            $form['linkedin_url'] = array( // Add field to form
  'profile_linkedIn url'             '#type' => 'blah''textfield',
                '#title' => t('Your LinkedIn profile');,
                '#default_value' => isset($linkedin_url) ? $linkedin_url : '',
                '#maxlength' => 255,
                '#size' => 60,              
                '#description' => t('Please enter your LinkedIn profile URL'),
                '#weight' => 10,
            );          
            $form['#submit'][] = 'entity_registration_custom_registration_form_submit'; // saveCall existingcustom usersubmit handler
user_save(        
    }
}


/**
*
* Custom Form Submit Handler
*
*/
function  MYMODULE_MYFORM_submit(object$form, &$form_state){
 array('uid' => $existingUser global $user;
    $profile = profile2_load_by_user($user->uid),
; // $edit,Load profile2 data
  'Personal Information' $profile['main']->field_linkedin_url['und'][0]['value'] =  $form_state['values']['linkedin_url']; // categorySet new value for Profile2 field
    profile2_save($profile['main']);
    drupal_set_message(t('Submit handler works!'));
}

Any suggestions? thx!

I have an idea but not sure will it work:

  1. Implement hook_field_attach_form to attach the field to event registration form and load value from Profile entity.
  2. Implement hook_field_attach_submit to update/save data to Profile entity.

I would use the code below to update user field:

// load user object
$existingUser = user_load('USERID');

 // create an array of properties to update
$edit = array(
  'profile_linkedIn url' => 'blah'
);

// save existing user
user_save(
  (object) array('uid' => $existingUser->uid),
  $edit,
  'Personal Information' // category
);

Any suggestions? thx!

Any suggestions? thx!

 

EDIT: I implemented this functionality. For those interested, complete source code is below: (Note that I'm saving data to profile2!)

/**
*
* Implements hook_form_alter
*
*/
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
    global $user;
    if ($form_id == 'registration_form') {
            $profile = profile2_load_by_user($user->uid); // Load profile2 data
            if ($profile['main']->field_linkedin_url) {
                $linkedin_url = $profile['main']->field_linkedin_url['und'][0]['value'];
            } 
            $form['linkedin_url'] = array( // Add field to form
                '#type' => 'textfield',
                '#title' => t('Your LinkedIn profile'),
                '#default_value' => isset($linkedin_url) ? $linkedin_url : '',
                '#maxlength' => 255,
                '#size' => 60,              
                '#description' => t('Please enter your LinkedIn profile URL'),
                '#weight' => 10,
            );          
            $form['#submit'][] = 'entity_registration_custom_registration_form_submit'; // Call custom submit handler
        
    }
}


/**
*
* Custom Form Submit Handler
*
*/
function  MYMODULE_MYFORM_submit($form, &$form_state){
    global $user;
    $profile = profile2_load_by_user($user->uid); // Load profile2 data
    $profile['main']->field_linkedin_url['und'][0]['value'] =  $form_state['values']['linkedin_url']; // Set new value for Profile2 field
    profile2_save($profile['main']);
    drupal_set_message(t('Submit handler works!'));
}
Source Link
Antonio
  • 407
  • 6
  • 20

update user's profile field from another form

Here is what I need to achieve:

  1. There is field "linkedIn url" in user's profile entity.
  2. There is Event registration form on the website.
  3. In the Event Registration form I would like to render field "linkedIn url" with value from the user's profile entity.
  4. When user submits the Event Registration I want to save a value of the field to the database (to user profile entity of course).

I have an idea but not sure will it work:

  1. Implement hook_field_attach_form to attach the field to event registration form and load value from Profile entity.
  2. Implement hook_field_attach_submit to update/save data to Profile entity.

I would use the code below to update user field:

// load user object
$existingUser = user_load('USERID');

// create an array of properties to update
$edit = array(
  'profile_linkedIn url' => 'blah'
);

// save existing user
user_save(
  (object) array('uid' => $existingUser->uid),
  $edit,
  'Personal Information' // category
);

Any suggestions? thx!