Hi,

I'm trying to programmatically set address fields while creating a new node.

My technique works for text (core) and email fields, but not for address fields. Any suggestions? How would you do this?

// When bootstrap_create_business function is called (business is a content type), phone and email are saved.  Addresses are not saved.  Why?

function bootstrap_create_business($config_all) {
  $config = $config_all['business'];
  $node = (object) array('type' => 'business');
  node_object_prepare($node);
  $node->uid = variable_get('uid_provider', 2);
  $node->title = $config['title'];
  $node->language = 'en';

  set_text_field($node, 'field_phone', $config['phone']);
  set_email_field($node, 'field_email', $config['email']);
  set_address_field($node, 'field_mailing_address', $config['mailing_address']);
  set_address_field($node, 'field_map_address', $config['map_address']);

  node_save($node);  // $node has address data
  $node = node_load($node->nid);  // after load, address data is gone.  Email and phone were saved.
  return $node;
}

function set_text_field(&$node, $field_name, $value, $format='text_plain', $key=0, $language=LANGUAGE_NONE) {
  $data[$language][$key]['format'] = $value;
  $data[$language][$key]['value'] = $value;
  $node->$field_name = $data;
}

function set_email_field(&$node, $field_name, $value, $format='text_plain', $key=0, $language=LANGUAGE_NONE) {
  $data[$language][$key]['email'] = $value;
  $node->$field_name = $data;
}

function set_address_field(&$node, $field_name, $value, $format='text_plain', $key=0, $language=LANGUAGE_NONE) {
  $defaults = addressfield_default_values(array(
      'available_countries' => array(),
      'default_country' => variable_get('site_default_country', 'US')));
  $value = array_merge($defaults, $value);

  $data[$language][$key] = $value;
  $node->$field_name = $data;
}

site_default_country = US

Comments

MichaelCole’s picture

Snap! I had the wrong field name.

Anyways, I still feel like this isn't the best/safest method. Am I missing something?

Mike

MichaelCole’s picture

Ok, sorry for the multiple updates.

This code "works" to put records in node, node_revision, field_address_map, field_address_mail, etc. tables.

When I view the node, I see the address.

When I edit the node, I don't see the address. That's a bug. What's up with that?

Mike

MichaelCole’s picture

Title: Addressfield doesn't save addresses correctly during node_save. » Saved Addressfield data appears in view, but not in edit.
rickmanelius’s picture

Same issue here, and it's preventing a fully working patch here #1023068: Feeds mapper for Address Field. So this patch apparently IS working, there is just some glitch in programmatically importing the address field information and then trying to edit it further.

rickmanelius’s picture

@MichaelCole

In issue #1023068: Feeds mapper for Address Field, I just found that it's required using the feeds importer to have the country mapping with a non-null country.

I bet what is happening is that addressfield is assuming a GUI input of the data, which would always give a default country. However, when the country is not specified during the import process, the function that loads the data for the edit view doesn't recognize and/or respect the piece of data and gives a blank form.

So my best bet would be to see if the load function for the default values can still show the pieces even if a country is not specified!

rickmanelius’s picture

Here's the answer in addressfield_field_widget_form

  if (!empty($form_state['addressfield'][$element_key])) {
    // Use the value from the form_state if available.
    $address = $form_state['addressfield'][$element_key];
  }
  elseif (!empty($items[$delta]['country'])) {
    // Else use the saved value for the field.
    $address = $items[$delta];
  }
  else {
    // Otherwise use the instance default.
    $address = (array) $instance['default_value'][0];
  }

So when importing data, if the country is not specified nor is the element key there, it'll simply load the default values array upon load even if other fields are populated. Not sure if there is a good way around this other than to always mandate some country default in the mapper or upon address field submit.

esclapes’s picture

subscribe

naught101’s picture

natuk’s picture

Version: 7.x-1.0-alpha1 » 7.x-1.0-beta2

Same problem here. An address inserted through the GUI appears in both edit and view. A manually added address (direct db insert due to migration of tons of data) appears in view but not the edit form.

rsgracey’s picture

...and if you save the node, the fields are wiped clean, and the data lost.

mkostir’s picture

I can confirm the same behavior as in #10.

jeremymcminn’s picture

I can confirm this same behaviour as #10 also. Any way around this - not overly urgent for me right now, but if someone knows a fix for this would be appreciated.

jeffschuler’s picture

If I remember correctly, @rsgracey's solution was to specify a country in all import data.

mkostir’s picture

My workaround at the moment is following (does not solve the problem though).

I made a set of ordinary textfields where I import the address detials first.
Than I made set of rules, going through the all imported nodes and for each I "set data value" from the text field to the addressfield.

It is some extra work, but for now, it works all good.

jeremymcminn’s picture

Hi Jeff

I included a country field and all my data had UK in that field but still the same problem. Will attempt again and see if I can get it working...

jeremymcminn’s picture

Jeff, stupid me had not mapped the country to the field - worked, however it didn't seem to include the Address: administrative area (State), in fact it completely removes the editing field too unless I go to edit and save it once and then go back into it it reappears.

Any idea what is happening there?

morelight’s picture

I also forgot to map the country;-(

that0n3guy’s picture

This is a pretty easy fix. I forgot to set a country and imported 2000+ nodes. So all I did was set the importer to "update" and mapped a column to the country field.

In my case, I didn't even have a country column in my csv, so I mapped some random unused column. Then I used feeds_tamper module to set the default value of that column. I just set everything to "US" (no quotes) and re-imported. It updated all my nodes and I am good to go.

edit: the "US" should have been capitalized.

Anonymous’s picture

Same issue here. I had to use the "country code" for the country mapping in order to resolve the issue. If you're providing a country and it is not working, be sure to use the "name" of the option in your country select list. For example, "Canada" is "CA" and "United States" is "US".

Use the feeds_tamper module if you need to modify your input sources to match this criteria. ;-)

johnv’s picture

Title: Saved Addressfield data appears in view, but not in edit. » Programmatically saved Addressfield data appears in view, but not in edit.

Marked #1945618: Inserting addresses into form programmatically breaks if country is different than default as duplicate:
Q: I create a dropdown of previous addresses to be inputted into the address field, which is in each line item(commerce). It works perfectly if the address being inputted has the same country as the default one in the form. If the country is different however, it refreshes the form and resets it to the default country & state/province.

A: Nevermind, I figured it out. Turns out that Drupal doesn't like it when you try to force the #options lists, so I simply had to use the form API to make the element that was giving me a problem this attribute: '#validated' => TRUE

johnv’s picture

Title: Programmatically saved Addressfield data appears in view, but not in edit. » Programmatically saved Addressfield data appears in view, but not in edit

Marked #1937114-2: data shows on node display, not on node edit, after import via Feeds as duplicate. But see Comment #2 which has relevantinfo regarding the country field, which may be changed in a new release:
"Our imported addresses do not have a country set, so the third statement (the default value) was being used. After adding a country everything worked as expected.

johnv’s picture

Title: Programmatically saved Addressfield data appears in view, but not in edit » Addressfield data appears in view, but not in edit (when saved programmatically or via Feeds) due to missing country subfield.

I guess the general pattern is: When no (default) country is present, the saved address is saved, but not correct.

There should be some error message, when saving an address without Country subfield.

rszrama’s picture

Title: Addressfield data appears in view, but not in edit (when saved programmatically or via Feeds) due to missing country subfield. » Create a watchdog error message when an address field value is saved without a country
Version: 7.x-1.0-beta2 » 7.x-1.x-dev
Category: bug » feature

I'm not sure there's a whole lot we can do here. If this is the crux of the issue, we don't have a way to effectively prevent the save of incomplete data. We could simply document the requirement, but that won't stop people from abusing the API. I think the best we can hope for is simply to throw an error message up in the watchdog and hope people notice it. Any better ideas?

tararowell’s picture

I don't think this is really a feature request to get an error message in this scenario - but leans more towards a bug. An error message that a required mapping feature is missing would save people the confusion of not knowing why there are field values in the database, but not on the profile edit form. The mapping works fine if it's done correctly. Also, some simple tips like, 'Use abbreviations for US State values or use the Feeds Tamper plugin' need to be added.

Any kind of error/help messaging would be fantastic!

bojanz’s picture

Status: Active » Closed (duplicate)

The patch in #968112: Allow addressfield to be optional will make sure that nothing is saved to the database if the country is not selected.