Hello,

I have run into a problem when programatically setting properties on addressfileld:

if ($profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile)) {
  $profile_wrapper->commerce_customer_address->postal_code->set($zipCode);
}

The code above will throw the following warning:

Warning: key() expects parameter 1 to be array, string given in addressfield_default_values() (line 326 of /srv/www/drupal/sites/all/modules/addressfield/addressfield.module).

It appears to be caused by the fact that addressfield_default_values is set as 'auto creation' callback and with the code as above this function is called as:

addressfield_default_values('postal_code')

This function expects it's $available_countries argument to be an array or NULL but in this case it is a string and will ultimately fail at line 326 which prevents from the value to be actually saved:

$default_country = key($available_countries);

I have changed the wrapping if statement to also check with is_array which resolved the problem:

// If the default country is undefined or not in the list of available countries,
// just fallback to the first country in the list.
if (!$default_country || (is_array($available_countries) && !isset($available_countries[$default_country]))) {
  $default_country = key($available_countries);
}

Comments

hawkeye.twolf’s picture

+1, I see this issue as well.

alexh58’s picture

+1 too!!! I've been following this one but have accepted just having a site_default_country variable set which does fix the issue.

rszrama’s picture

Title: addressfield_default_values needs to check if $available_countries is an array » Use a wrapper around addressfield_default_values() as the Entity API auto creation callback
Version: 7.x-1.0-beta3 » 7.x-1.x-dev
Status: Active » Fixed

The fix isn't to change the way addressfield_default_values() works but to add a wrapper around the function for use as an auto creation callback. The Entity API doesn't document the parameters, but the first one it tries to pass to an auto creation callback is a property name.

Commit: http://drupalcode.org/project/addressfield.git/commitdiff/2f4b17e

Status: Fixed » Closed (fixed)

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