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
Comment #1
MichaelCole commentedSnap! I had the wrong field name.
Anyways, I still feel like this isn't the best/safest method. Am I missing something?
Mike
Comment #2
MichaelCole commentedOk, 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
Comment #3
MichaelCole commentedComment #4
rickmanelius commentedSame 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.
Comment #5
rickmanelius commented@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!
Comment #6
rickmanelius commentedHere's the answer in addressfield_field_widget_form
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.
Comment #7
esclapes commentedsubscribe
Comment #8
naught101 commentedrelated: #968112: Allow addressfield to be optional
Comment #9
natukSame 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.
Comment #10
rsgracey commented...and if you save the node, the fields are wiped clean, and the data lost.
Comment #11
mkostir commentedI can confirm the same behavior as in #10.
Comment #12
jeremymcminn commentedI 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.
Comment #13
jeffschulerIf I remember correctly, @rsgracey's solution was to specify a country in all import data.
Comment #14
mkostir commentedMy 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.
Comment #15
jeremymcminn commentedHi 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...
Comment #16
jeremymcminn commentedJeff, 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?
Comment #17
morelight commentedI also forgot to map the country;-(
Comment #18
that0n3guy commentedThis 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.
Comment #19
Anonymous (not verified) commentedSame 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. ;-)
Comment #20
johnvMarked #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
Comment #21
johnvMarked #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.
Comment #22
johnvI 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.
Comment #23
rszrama commentedI'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?
Comment #24
tararowell commentedI 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!
Comment #25
bojanz commentedThe patch in #968112: Allow addressfield to be optional will make sure that nothing is saved to the database if the country is not selected.