0

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).

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!'));
}
1
  • Anyone to help with this one?
    – Antonio
    Commented Nov 9, 2013 at 16:31

1 Answer 1

1

You have to modify your form in a hook_form_alter : and the linked_in url will appear in the event registration form

function mymodule_form_alter(&$form, &$form_state, $form_id){
   if ($form_id == 'eventregistration_node_form'){

      // Find the user obj
      global $user;
      // Load all profile fields
      $account = user_load($user->uid);
      // Add the linked_in url in the event registration form
      // Supposing your field madhine name is ; linkedin_url
      // Supposing you use the link module for this field (if not, change ['url'] to ['value']
      $form['linkedin_url']['und'][0]['url']['#default_value'] = $user->linkedin_url['und'][0]['url'];
   }
}

If you don't want to expose the linked_in field during the form do this :

function mymodule_form_alter(&$form, &$form_state, $form_id){
   if ($form_id == 'eventregistration_node_form'){
     // Hide your field
     $form['linkedin_url'][und]['#access']  = false;
     //Add another submit function
      $form['actions']['submit']['#validate'][] = 'mymodule_eventregistration_validate_form_alter';
   }
}

// Your submit function
function mymodule_eventregistration_validate_form_alter($form, $form_alter){

     // Find the user obj
      global $user;
      // Load all profile fields
      $account = user_load($user->uid);
      // Add the linked_in url in the event registration form
      // Supposing your field madhine name is ; linkedin_url
      // Supposing you use the link module for this field (if not, change ['url'] to ['value']
      $linkedin_url = $user->linkedin_url['und'][0]['url'];

    form_set_value($form['linkedin_url'], array('und' => array(0 => array('url' => $linkedin_url))), $form_state);
}
3
  • that's great! Thx for pointing me in the right direction. As far as I understand, the above's example saves the linkedin_url to the event node, but I want linkedin_url to be saved to User entity instead. What hook can I use for that? Also how can I avoid saving linkedin_url to event node? THX!
    – Antonio
    Commented Nov 11, 2013 at 2:21
  • 1
    In this case, use the second example and add not a 'validate' function, but a 'submit' function. Il this submit function, load the user ($account = user_load($user->uid)) and add the linked_in url from the form value ($account-> linkedin_url['und'][0]['url'] = $form_state['values']['linkedin_url']['und'][0]['url']), and save the user (user_save($account)); Commented Nov 11, 2013 at 7:50
  • That's it! Thank you bobylapointe! You are the man! :)
    – Antonio
    Commented Nov 11, 2013 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.