If you have the address field set to not required in cck, and a user creates a node, the country code will be set to 'us'. I searched the code for a fix, then gave up. There is a variable called 'addresses_country_default' that I set to the empty string, thinking that would do the trick, but it didn't. The workaround I found was to configure the field with a default PHP value (field configuration page, under Default value > PHP code) as follows:

return array(
  0 => array('is_primary' => 0, 'aname' => '', 'country' => '', 'province' => '', 'city' => '', 'street' => '', 'additional' => '', 'postal_code' => ''),
);

Hope that helps someone.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tugis’s picture

I have the same problem and your solution didn't work here. I changed in the module's code the parts where "US" was set as default to an empty string but it's still the same issue even after clearing caches.

Btw, I don't understand why you the country default value was set to US when the field is not required. In a situation like this it should just be empty I guess...

dwightaspinwall’s picture

Try this. Set the address components to NULL, not empty string, as in:

return array(
    0 => array('is_primary' => 0, 'aname' => NULL, 'country' => NULL, 'province' => NULL, 'city' => NULL, 'street' => NULL, 'additional' => NULL, 'postal_code' => NULL),
);

also, set the country default like so (this may need to be an update, not an insert in your case):

function your_module_update_6xxx() {
  $ret = array();
  // Addresses module provides no config UI for this
  $data = 's:0:"";';
  $ret[] = update_sql("INSERT INTO {variable} (name, value) VALUES ('addresses_country_default', '$data')");
  return $ret;
}

Unfortunately, I don't know specifically what's going on in the code. If I had the time, I'd do some refactoring...

usonian’s picture

I ran into this as well with version 6.x-1.09; patching the code to default the country to an empty string has no effect, and I spent a fair amount of time trying to figure out where exactly the default value of 'us' is getting set without success. Very peculiar.

dwightaspinwall's fix (setting the default field values to NULL with PHP in each CCK address field defined) in #2 seems to work.

However, I encountered an additional problem with the theme_addresses function; even when the fields are all empty it still returns a stray string consisting of a comma. I wound up patching the beginning of theme_addresses to look like:

  $empty_address = TRUE;
  foreach (array_values($afields) as $field) {
    if (!empty($field)) {
      $empty_address = FALSE;
    }
  }

  // If all fields are hidden, return ''
  if ($empty_address || empty($afields)) {
    return '';
  }

(I know it would be more proper to override the function in a custom theme or module, but as I have already patched the addresses module for an unrelated issue I went ahead and fixed it here.)

usonian’s picture

Eureka!

I spent some time trying to trace where exactly in the CCK save process the default of 'us' was getting set, and concluded that it simply wasn't - which led me to check the MySQL schema, and sure enough, the default value for the '_country' column in the database schema is 'us'.

Hopefully I can figure out exactly where that's getting declared, and roll a patch tonight.

AlexisWilke’s picture

addresses.module around line 60.

But I'm not too sure you can safely change that default.

Let us know what you find.

Thank you.
Alexis

usonian’s picture

Attached is a patch for version 6.x-1.10 to the addresses_addressesfieldapi() function that sets the default value of 'country' to '' instead of 'us'. The default value of 'us' is actually getting built into the database schema when a new addresses_cck field is added to a content type, meaning that even if someone submits a field with a deliberately empty value for the 'Country' part of an address, the record created in the database gets a value of 'us', which has caused us several headaches.

If you have an existing install with this problem it may be easiest to tweak your DB schema directly to change the default field value to an empty string - AFTER BACKING UP YOUR DATA, of course.

usonian’s picture

Hmm, although this patch should in theory work for new installations of the module/new addresses_cck field instances, updating the schema of an existing content_type_xxx table with an addresses_cck field does not fix the problem; Looking in the 'db_columns' field of the 'content_node_field' table I can see the default value of 'us' still there.

update
Ok, I figured it out. The 'db_columns' field gets built whenever you save a field definition, so in my case the address fields were was still hanging on to the old default value of 'us' even after I had changed the table schema. Here are the steps I took to fix the problem for good. I'm not familiar enough with CCK internals to know if I'm committing grave sacrilege, but this seems to be working for me:

1. Patch addresses_addressesfieldapi() to set the default country value to '' instead of 'us'.
2. Update the DB schema of the affected content type(s) to set the default country value to '' instead of 'us' (I did this directly on the database, but the proper way would be to do it with hook_update_N() in a custom module)
3. For each address field, re-edit & save (even though I'd already set the defaults as above.) - That cleared out the default in content_type_xxx.db_columns.

Now when I save a CCK node with an address field whose country value is left empty, the saved value is an empty string.

AlexisWilke’s picture

You may want to search for other instances of variable_get('addresses_country_default', 'us');

The CCK code is not easy to work with also...

And we have the problem that the province requires the country to show up. (i.e. if you are in Australia and only want to see to Australian [say you sell fish... and cannot ship it overseas], then you cannot hide just the country and hope to get the province part working right.)

dgsiegel’s picture

if you work with features, you don't have this problem. it is easy enough to just delete that variable and you're done. of course, all previous saved nodes will have "united states" in their fields, if they did not change it

derhasi’s picture

Status: Active » Needs review
FileSize
2.1 KB

Attached is a patch (extending usonian's patch) that should also fix the mentioned problems in #7, as it uses the content crud function to update the field definition (and so database schema).

AlexisWilke’s picture

Status: Needs review » Active

derhasi,

Great! I checked that in. I'll leave the issue open, just in case others have problems with the change.

Thank you.
Alexis Wilke

See http://drupalcode.org/project/addresses.git/commit/2637b3c