1

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.

  1. 'From' addressfield populated from the 'User addressfield' attached to my 'user' entity type.

  2. '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...

3 Answers 3

1

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(&$form, &$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']->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;
  }
}
3
  • In your answer where is the reference to the user addressfield? Jul 2, 2014 at 8:21
  • The above code sets the fields the variables that are named as they they appear in the front end. Each variable is currently being set to a string, you will need to replace the string with the values that you want. To get the values from the current user grab the user id from Global and do a user_load if you print the array that is returned to the screen or watchdog you will be able to see how to access the data that you need.
    – pete80
    Jul 2, 2014 at 8:41
  • Ok thanks alot, +1 for your edits, i've changed the code i'm using to reflect what i've learned. Unfortunately it's still not going through. When I pass my current_user address_fields value am I right in using ['value'] or is this a placeholder? Jul 2, 2014 at 17:19
0

You can use the default value to set it - see here for an example: https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#default_value

<?php 

         $form['body'] = array(

          '#type' => 'textarea', 
          '#title' => t('Body'), 
          '#default_value' => $node->body,  //$user->addressField or $node->school_address
          '#required' => TRUE,
         );

  ?>
0

This may be a duplicate of this question and I posted there a working solution for one node field, should be very easy to add the second field.

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.