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(&$form, &$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']->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->uid); //Populate the entityform addressfield with values from the user addressfield $form['field_user_address'][LANGUAGE_NONE][0]['#address']['thoroughfare'] = $current_user->field_user_address[LANGUAGE_NONE][0]['#address']['thoroughfare']['value']; $form['field_user_address'][LANGUAGE_NONE][0]['#address']['premise'] = $current_user->field_user_address[LANGUAGE_NONE][0]['#address']['premise']['value']; $form['field_user_address'][LANGUAGE_NONE][0]['#address']['locality'] = $current_user->field_user_address[LANGUAGE_NONE][0]['#address']['locality']['value']; $form['field_user_address'][LANGUAGE_NONE][0]['#address']['administrative_area'] = $current_user->field_user_address[LANGUAGE_NONE][0]['#address']['administrative_area']['value']; $form['field_user_address'][LANGUAGE_NONE][0]['#address']['postal_code'] = $current_user->field_user_address[LANGUAGE_NONE][0]['#address']['postal_code']['value']; } } } ... Ok I got this working:
global $user; $user_fields = user_load($user->uid); //useful ... dsm($user_fields); $userthoroughfare = $user_fields->field_user_address['und']['0']['thoroughfare']; $userpremise = $user_fields->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...