Here is what I need to achieve:
- There is field "linkedIn url" in user's profile entity.
- There is Event registration form on the website.
- In the Event Registration form I would like to render field "linkedIn url" with value from the user's profile entity.
- When user submits the Event Registration I want to save a value of the field to the database (to user profile entity of course).
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!'));
}