How to dynamically Prepopulate / Prefill addressfields I have an single entityform of type 'letter generator' this contains two addressfields: 


'To' addressfield
'From' addressfield


I would like to prepopulate / prefill these addresses with content from elsewhere on my site.


'From' addressfield populated from the 'User addressfield' attached to my 'user' entity type.
'To' addressfield populated from the 'School addressfield' attached to my 'School  info' 'node' entity type (also the parent node for the letter generator)


I am new to php and programming so this is my first attempt at a custom module. So far I have the following php that attempts to address point (1.)

function letter_address_form_alter(&amp;$form, &amp;$form_state, $form_id) {

// Check to see if the form alter function is actually running
dsm($form);

    // Only do this for a new, not-yet updated entityform -NB might need a 'return' here
    if (!isset($form['#entity']-&gt;is_new)) {

        //Ensure, we're updating field info on the correct entity type
        if ($form_id == 'letter_generator_entityform_edit_form') {

            // load user information
            global $user;
            $current_user = user_load($user-&gt;uid);

            //Populate the entityform addressfield with values from the user addressfield
            $form['field_user_address'][LANGUAGE_NONE][0]['#address']['thoroughfare'] = 
                $current_user-&gt;field_user_address[LANGUAGE_NONE][0]['#address']['thoroughfare']['value'];
            $form['field_user_address'][LANGUAGE_NONE][0]['#address']['premise'] =
                $current_user-&gt;field_user_address[LANGUAGE_NONE][0]['#address']['premise']['value'];
            $form['field_user_address'][LANGUAGE_NONE][0]['#address']['locality'] =
                $current_user-&gt;field_user_address[LANGUAGE_NONE][0]['#address']['locality']['value'];
            $form['field_user_address'][LANGUAGE_NONE][0]['#address']['administrative_area'] =
                $current_user-&gt;field_user_address[LANGUAGE_NONE][0]['#address']['administrative_area']['value'];
            $form['field_user_address'][LANGUAGE_NONE][0]['#address']['postal_code'] =
                $current_user-&gt;field_user_address[LANGUAGE_NONE][0]['#address']['postal_code']['value'];

        }
    }

}


...
Ok I got this working:

global $user;
$user_fields = user_load($user-&gt;uid);
//useful ...
dsm($user_fields);
$userthoroughfare = $user_fields-&gt;field_user_address['und']['0']['thoroughfare'];
$userpremise = $user_fields-&gt;field_user_address['und']['0']['premise'];
$form['field_user_address'][LANGUAGE_NONE][0]['#address']['thoroughfare'] = $userthoroughfare;
$form['field_user_address'][LANGUAGE_NONE][0]['#address']['premise'] = $userpremise;



  NB adding ['value'] to the line:


$form['field_user_address'][LANGUAGE_NONE][0]['#address']['administrative_area']['value'] = $useradministrativearea;



  will select the state for you...
I cant find any reference of hook_form_entityform_alter if it is available you will want to take out the hook part and replace this with you module name.

I have re written the code using hook_form_alter. You will need to replace $address1 etc with your own code.

function letter_addresses_form_alter(&amp;$form, &amp;$form_state, $form_id) {

// Ensure, we're updating field info on the correct entity type
//This needs to be more specific incase more entity forms are created
if ($form_id == 'yourformname_entityform_edit_form') {

// Only do this for a new, not-yet updated entityform -NB might need a 'return' here
if (!isset($form['#entity']-&gt;is_new)) {
//Populate the entityform addressfield with values from the user addressfield
$address1 = "address1";
$address2 = "address2";
$county = "county";
$postcode = "postcode";
$form['field_address'][LANGUAGE_NONE][0]['#address']['thoroughfare'] = $address1;
$form['field_address'][LANGUAGE_NONE][0]['#address']['premise'] = $address2;
$form['field_address'][LANGUAGE_NONE][0]['#address']['locality'] = $county;
$form['field_address'][LANGUAGE_NONE][0]['#address']['administrative_area'] = $county;
$form['field_address'][LANGUAGE_NONE][0]['#address']['postal_code'] = $postcode;
  }
}