The default country value is lost when upgrading from 7.x-1.0-rc1 to 7.x-1.0.

With 7.x-1.0-rc1 I have a "Postal address" field with the following under "Default value":
Country: United States
Address 1:
Address 2:
City:
State: - None -

After upgrading to 7.x-1.0 and applying database updates (7001):
Default country: - None -

Comments

bojanz’s picture

Category: Bug report » Support request
Status: Active » Fixed

Guessing your field was optional ("Required" wasn't selected).
The update function sets the default country to "- None - " for optional fields, so that by default the addressfield is saved empty unless the customer selects the country and enters the address.

You're free to set your field as "required", or select a default country (showing it selected by default, but still allowing the customer to select "None" and avoid selection).

aohrvetpv’s picture

Category: Support request » Bug report
Status: Fixed » Active

Thanks for the response. The field that loses the default value is actually required.

Steps to reproduce:

  1. Install Drupal.
  2. Install Address Field 7.x-1.0-rc1.
  3. Add content type "foo".
  4. Add 'Postal address' field "bar" to "foo":
    • Required field: checked
    • Default value->Country: United States
  5. Browse to node/add/foo.
  6. Observe that field "bar" has default value "United States".
  7. Upgrade to Address Field 7.x-1.0. Apply database updates and clear cache.
  8. Browse to field "bar" edit page.
  9. Observe that field "bar" has Default country: - None - (Expected: "United States")
  10. Browse to node/add/foo.
  11. Observe that field "bar" has default value "- Select -". (Expected: "United States")

This seems to me to be a bug, because a specific configuration setting made in 7.x-1.0-rc1 is lost without warning. The field becomes incorrect after upgrade--its default value has changed from what it was set to be.

bojanz’s picture

There's a bug then, thanks for confirming.

Bios450’s picture

I have the same problem ...

Tell me how to solve it? or this bug will be fixed in the next version?

bojanz’s picture

@Bios450
The data has been lost so it can't be fixed, you need to edit your field and set the default country again.

Bios450’s picture

@bojanz
Country I have already done ...
whether I can put the city by default?

bojanz’s picture

@Bios450
That is no longer possible through the UI, you will need to use hook_addressfield_default_values_alter(), as documented in addressfield.api.php

aohrvetpv’s picture

Is the better remedy to improve the existing update function so it preserves default country, or just force administrators to re-enter the default country?

I don't see any downside to improving the update function. Users who already updated will be out of luck, but it could prevent a problem for users who have not yet updated.

bojanz’s picture

@AohRveTPV
We need to fix the update function for sure. Not arguing with that.

redeight’s picture

Also, shouldn't it default to the only available country when only one country is allowed? Seems really bizarre to have the only options be "none" and "COUNTRY A"... Seems like it should just use "COUNTRY A" in that case

bojanz’s picture

@RedEight
The "- None -" option is there so that you can indicate that the field is empty. If you set the field as required, that option won't be there.

redeight’s picture

Are you sure? I've set the field to required and it still shows the country box and defaults selection to none. I will note that I am doing this inside of a webform through addressfield_tokens. So it may be an incompatibility on their end. Since the new country selector shows up I think it has to do with this module rather than that one.

ReBa’s picture

For me, #2 worked perfectly.

Had 1 country selectable, together with 1 option that was "none" after the update.
Changing the default value worked, if anything has been updated before, data was lost.

Thanks Bojanz.

devad’s picture

PHP log message notice for this issue:

Notice: Undefined index: country in addressfield_update_7001() (line 206 of /home/mysite/public_html/sites/all/modules/addressfield/addressfield.install).

owenpm3’s picture

I'm also not getting a default country even though I've set a default country and marked it as required. This error comes up.

Warning: Missing argument 2 for addressfield_default_values(), called in C:\Bitnami\wampstack-5.4.37-0\apache2\htdocs\sbe\sites\all\modules\addressfield_tokens\addressfield_tokens.components.inc on line 120 and defined in addressfield_default_values() (line 375 of C:\Bitnami\wampstack-5.4.37-0\apache2\htdocs\sbe\sites\all\modules\addressfield\addressfield.module).
Notice: Undefined variable: instance in addressfield_default_values() (line 376 of C:\Bitnami\wampstack-5.4.37-0\apache2\htdocs\sbe\sites\all\modules\addressfield\addressfield.module).
Notice: Undefined index: bundles in _addressfield_country_options_list() (line 874 of C:\Bitnami\wampstack-5.4.37-0\apache2\htdocs\sbe\sites\all\modules\addressfield\addressfield.module).
Warning: Invalid argument supplied for foreach() in _addressfield_country_options_list() (line 874 of C:\Bitnami\wampstack-5.4.37-0\apache2\htdocs\sbe\sites\all\modules\addressfield\addressfield.module).
Notice: Undefined variable: instance in addressfield_default_values() (line 377 of C:\Bitnami\wampstack-5.4.37-0\apache2\htdocs\sbe\sites\all\modules\addressfield\addressfield.module).
Notice: Undefined variable: instance in addressfield_default_values() (line 404 of C:\Bitnami\wampstack-5.4.37-0\apache2\htdocs\sbe\sites\all\modules\addressfield\addressfield.module).

  • bojanz committed 7dfed52 on 7.x-1.x
    Issue #2412989: Country default value lost when upgrading
    
bojanz’s picture

Status: Active » Fixed

I've pushed a fix for the update function.

I'll commit a fix for people who already lost the default_country in #2415627: Empty default_country causes issues when the field is required.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.