I'm trying to pre-populate a field in a new piece of content with a country field from a users profile. These are two different Drupal fields, but they are both using the location module so country codes should easily transferred between the two.

I have managed to verify the data from $field_country_you_co_ordinate_fo['und'][0]['country']), this prints the country code 'gb' as expected.

I can't pull the value into $form['field_location']. Tried several different chains of array keys based on the previous ticket linked at the bottom. I'm clearly misunderstanding the combinations of keys needed to pull from one variable to another, so any help would be greatly appreciated.

Thanks in advance

function perma_boot_form_organisation_node_form_alter(&$form, &$form_state, $form_id) {
  // Only run against newly created node 

  if (empty($form['nid']['#value']) && (!isset($form['#node']->op))) {
    global $user;
    $user_fields = user_load($user->uid);

    if (isset($user_fields->field_country_you_co_ordinate_fo['und'][0]['country'])) {
        //print_r ($user_fields->field_country_you_co_ordinate_fo['und'][0]['country']);
        $form['field_location']['und'][0]['country'] = $user_fields->field_country_you_co_ordinate_fo['und'][0]['country'];

Have been working from the previous example: How to pre-populate fields on a node creation form based on fields from Profile2 module?

  • You can use Devel module to print structure of unknown object Sep 7, 2015 at 16:42
  • You can also use a similar line to your commented out print_r statement to print the structure of the $form array no? Then you will probably see that you're simply misunderstanding the array structure and putting the value in the wrong place. (I think you're missing the #address element of the array: $form['field_location']['und'][0]['#address']['country'])
    – Beebee
    Sep 7, 2015 at 23:07
  • Hello and welcome. :) The structure of the field array depends on the type, number of values, etc (inspecting with Devel can be a big help, as @ar7max suggested). You're probably looking at something more along the lines of $form['field_location'][LANGUAGE_NONE][0]['country]['#default_value'] if you are indeed prepopulating with a default value. Cheers - Sep 7, 2015 at 23:19

1 Answer 1


Firstly, thanks to all for the help. I followed your advice and configured devel to print $page. Followed the structure of the page and came up with the amended the code:


Leaving Devel enabled, I can see the value has been set correctly to gb. However, this breaks the google map functionality.

On to the next problem :)

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.